<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: cgokey</title>
    <description>The latest articles on DEV Community by cgokey (@cgokey).</description>
    <link>https://dev.to/cgokey</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F861137%2F55525cac-b5ee-4d5c-8a1a-a69ea0affe52.png</url>
      <title>DEV Community: cgokey</title>
      <link>https://dev.to/cgokey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cgokey"/>
    <language>en</language>
    <item>
      <title>observed data reverting back in mobx-react with bootstrap-4</title>
      <dc:creator>cgokey</dc:creator>
      <pubDate>Wed, 11 May 2022 21:01:41 +0000</pubDate>
      <link>https://dev.to/cgokey/observed-data-reverting-back-in-mobx-react-with-bootstrap-4-3do2</link>
      <guid>https://dev.to/cgokey/observed-data-reverting-back-in-mobx-react-with-bootstrap-4-3do2</guid>
      <description>&lt;p&gt;I've been seeing an issue in mobx-react with bootstrap-4 and I'm getting some unexpected results that is driving me crazy.   I've boiled it down to the simple example below.&lt;br&gt;&lt;br&gt;
1) &lt;code&gt;Model&lt;/code&gt; object that simply holds an array of &lt;code&gt;data&lt;/code&gt;.  And an &lt;code&gt;action&lt;/code&gt; that provides a way to add &lt;code&gt;data&lt;/code&gt; to the array.&lt;br&gt;
2) &lt;code&gt;App&lt;/code&gt; component that instantiates the model object and will render:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the model's &lt;code&gt;data&lt;/code&gt; which is an array rendered as JSON string (that data is marked as &lt;code&gt;observable&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;A button to add a new object to model (the add is marked as an &lt;code&gt;action&lt;/code&gt; in the model object)
All this works fine, if I don't wrap the &lt;code&gt;Button&lt;/code&gt; in a bootstrap4 &lt;code&gt;Form&lt;/code&gt;, like below:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render() {    
    return (
      &amp;lt;&amp;gt;
          {JSON.stringify(this.model.data)}

          &amp;lt;Button onClick={this.addNewItem}&amp;gt;Add Another&amp;lt;/Button&amp;gt;      
      &amp;lt;/&amp;gt;
    )
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;But if the &lt;code&gt;Button&lt;/code&gt; is wrapped in a bootstrap4 &lt;code&gt;Form&lt;/code&gt;, you can see a slight delay where the JSON is changed to include the new item, but then instantly reverts back to the old data which does not include the new item.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render() {    
    return (
      &amp;lt;&amp;gt;
          {JSON.stringify(this.model.data)}

        &amp;lt;Form&amp;gt;
              &amp;lt;Button onClick={this.addNewItem}&amp;gt;Add Another&amp;lt;/Button&amp;gt;
        &amp;lt;/Form&amp;gt;


      &amp;lt;/&amp;gt;
    )
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any idea what might be going on here?   Full Source is below.&lt;br&gt;
Thanks,&lt;br&gt;
Chris&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { Component } from 'react';
import { observer } from 'mobx-react'
import { Card, Form, Button } from 'bootstrap-4-react';
import { action, makeObservable, observable } from 'mobx';

class Model {
  data = null
  constructor(data) {
      this.data = data

      makeObservable(this, {
          data: observable,
          add: action
      })
  }
  add() {
      this.data.push({ "Date": new Date() })
  }
}

class App extends Component {

  constructor(props) {
    super(props)
    this.addNewItem = this.addNewItem.bind(this);
    var data = [
      {
        "Date": new Date()
      }
    ]
    this.model = new Model(data)
  }

  addNewItem() {
    this.model.add()
  }

  render() {    
    return (
      &amp;lt;&amp;gt;
          {JSON.stringify(this.model.data)}

        &amp;lt;Form&amp;gt;
              &amp;lt;Button onClick={this.addNewItem}&amp;gt;Add Another&amp;lt;/Button&amp;gt;
        &amp;lt;/Form&amp;gt;


      &amp;lt;/&amp;gt;
    )
  }
}

export default observer(App);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
