<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Zac Jones on Medium]]></title>
        <description><![CDATA[Stories by Zac Jones on Medium]]></description>
        <link>https://medium.com/@zacjones?source=rss-6d566fbf5057------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*XNz5JPMUytRkl0f6.jpg</url>
            <title>Stories by Zac Jones on Medium</title>
            <link>https://medium.com/@zacjones?source=rss-6d566fbf5057------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 02:04:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zacjones/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Handle Multiple Inputs in React with ES6 Computed Property Name]]></title>
            <link>https://medium.com/@zacjones/handle-multiple-inputs-in-react-with-es6-computed-property-name-e3d41861ae46?source=rss-6d566fbf5057------2</link>
            <guid isPermaLink="false">https://medium.com/p/e3d41861ae46</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Zac Jones]]></dc:creator>
            <pubDate>Sun, 22 Oct 2017 22:15:10 GMT</pubDate>
            <atom:updated>2019-02-10T16:32:26.878Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://github.com/facebookincubator/create-react-app">create-react-app</a> (CRA) was used to generate the necessary boilerplate to start this React application. If you haven’t used it before, you should! There is zero configuration and you won’t need to touch Webpack or Babel. 😄</p><p>Here’s a great tutorial on CRA if you haven’t heard about it before: <a href="https://egghead.io/lessons/react-bootstrap-a-react-application-through-the-cli-with-create-react-app">Bootstrap a React Application through the CLI with Create React App</a></p><h4>Controlled Input</h4><p>The standard way React handles user input is through Controlled Inputs. The React component that renders the form defines a function that determines what we do with user input in that form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TqXXxFGOwJAfHURCW__Xww.png" /><figcaption>Controlled Input</figcaption></figure><p>In the above example, user input is being handled by the functionhandleChange which sets the components state variable, userInput, to the string typed by the user. userInput is then set as the value of the input.</p><p>The resulting output is rendered to the screen for feedback.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*YWZ01vIuqJ4GAbPyjMo4FQ.png" /><figcaption>Output</figcaption></figure><h4>The Problem of scaling Controlled Inputs</h4><p>The problem that we run into is when we want to add more and more input fields into our Component is that it becomes needlessly verbose. The intuitive way to add more inputs (at-least for me) is to just keep adding functions to handle the extra inputs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*joxZCyOZcWdKuuq0IIk7mA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-eG7jKDjPqvRMgOyqSHq0g.png" /><figcaption>It works</figcaption></figure><p>Each input has it’s own ‘handleChange’ function which works but we broke one of the biggest rules in software development, DRY.</p><p>Don’t Repeat Yourself! We just did that 2 times. 😳</p><p>Other than the state variables being updated, these functions are the same. There has to be a better way to do this.</p><h4>ES6 Computed Property Name</h4><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer">ES6 Computed Property Name</a>’s solve this exact problem for us. We can set a name property on each input and access that property dynamically by using the bracket [] syntax of Computed Property Names.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-wGOfdWd3ZXBOrSwwVL_og.png" /><figcaption>Note that the name is the same as the value it is updating</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*tt23_iKVpUdqilS5uCQaNg.png" /><figcaption>Works again!</figcaption></figure><p>We now have all our inputs handled by a single function that will take in the name of the input and update the corresponding state value.</p><p>We can even clean up our handleChange function by using a little more destructuring.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*jCkUA9K8IexwhHuMLyn3EQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3d41861ae46" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>