<?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: Nate</title>
    <description>The latest articles on DEV Community by Nate (@nbrustein).</description>
    <link>https://dev.to/nbrustein</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%2F144350%2F3d2e285a-6125-4f82-928e-5eef097df444.png</url>
      <title>DEV Community: Nate</title>
      <link>https://dev.to/nbrustein</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nbrustein"/>
    <language>en</language>
    <item>
      <title>What is the best way to update a property on a model using React hooks</title>
      <dc:creator>Nate</dc:creator>
      <pubDate>Mon, 11 Mar 2019 14:13:04 +0000</pubDate>
      <link>https://dev.to/nbrustein/what-is-the-best-way-to-update-a-property-on-a-model-using-react-hooks-ajc</link>
      <guid>https://dev.to/nbrustein/what-is-the-best-way-to-update-a-property-on-a-model-using-react-hooks-ajc</guid>
      <description>

&lt;p&gt;I'm switching from angularjs to react, and still learning the basic philosophy behind react.&lt;/p&gt;

&lt;p&gt;I ran into an issue trying to bind an input element to a property on a model.  I first tried using &lt;code&gt;useReducer&lt;/code&gt;, which worked fine to update the model.  The problem is that it does not trigger a re-render of the component, since the model is still the same model, just with an updated property.  As far as react is concerned, nothing has changed, so it does not need to re-render.&lt;/p&gt;

&lt;p&gt;I came up with two different solutions.  The first is to clone the model on each update.  In some cases this is fine, and maybe it's the functional programming way of doing things (I'm almost as new to functional programming as I am to React).  But it seems like a waste to go around cloning things all the time.&lt;/p&gt;

&lt;p&gt;The second solution is to use the &lt;code&gt;forceUpdate&lt;/code&gt; strategy posted at &lt;a href="https://medium.com/crowdbotics/how-to-use-usereducer-in-react-hooks-for-performance-optimization-ecafca9e7bf5"&gt;https://medium.com/crowdbotics/how-to-use-usereducer-in-react-hooks-for-performance-optimization-ecafca9e7bf5&lt;/a&gt;.  If I wrap that thing up in a nice custom hook, I can make it pretty clean.  But I know that forceUpdate is not really recommended, and I'm not comfortable enough yet with react to know if this is a reasonable place to use it.&lt;/p&gt;

&lt;p&gt;I've got everything laid out clearly in a jsfiddle at &lt;a href="https://jsfiddle.net/nbrustein/odjwkbvs/31/"&gt;https://jsfiddle.net/nbrustein/odjwkbvs/31/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, what should I be doing here?  Is the solution:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Stop worrying and love the clone.  It's what all the cool functional programmers are doing these days.&lt;/li&gt;
&lt;li&gt;Use forceUpdate.  This is what it's for.&lt;/li&gt;
&lt;li&gt;Something else I haven't thought of yet.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks in advance for any insights.&lt;/p&gt;


</description>
      <category>react</category>
      <category>reacthooks</category>
    </item>
  </channel>
</rss>
