<?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 Steven Boutcher on Medium]]></title>
        <description><![CDATA[Stories by Steven Boutcher on Medium]]></description>
        <link>https://medium.com/@stevenboutcher?source=rss-17d19ac0d125------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*L75SHhpZ7NI8wYVZV4o46A.jpeg</url>
            <title>Stories by Steven Boutcher on Medium</title>
            <link>https://medium.com/@stevenboutcher?source=rss-17d19ac0d125------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 06:47:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@stevenboutcher/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[Changing State In React Using setState()]]></title>
            <link>https://medium.com/@stevenboutcher/changing-state-in-react-using-setstate-49102dcfd25?source=rss-17d19ac0d125------2</link>
            <guid isPermaLink="false">https://medium.com/p/49102dcfd25</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[setstate]]></category>
            <dc:creator><![CDATA[Steven Boutcher]]></dc:creator>
            <pubDate>Tue, 22 Jan 2019 16:44:27 GMT</pubDate>
            <atom:updated>2019-01-22T16:44:27.216Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>It’s Confusing, I Know</strong></p><p>Once you learn how to initialize state in React, you’re probably feeling pretty cool. “Great,” you’re thinking, “state is a key concept in React and I’m actually <em>getting it</em>!”</p><p>And then you get to setting and changing state. There are all these new moving parts that you have to use and it’s just so frustrating to piece it all together! What’s that binding statement about? Do I need an event handler to set the state? These questions are important and I’m going to address them in a way I feel is less technical and more — for lack of better words, human.</p><p><strong>COOKIES</strong></p><p>For this article, I thought it would be a fun and useful analogy to compare a React component to a Cookie Jar, and to compare the state of that Cookie Jar to the jar’s contents. In my opinion, this analogy works because:</p><ul><li>The amount of cookies in the jar is subject to change (state can update)</li><li>You can’t just change the amount of cookies without opening the jar first (state is protected from direct changes)</li><li>When you bake more cookies — or eat some — you don’t just go buy a new jar. You change the contents of the existing cookie jar. (state can be modified indirectly, not reassigned a new object directly)</li><li>You can choose which kind of cookie you want to add or take (you can update as many or as few properties in state as you want)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*6mW5cHhVQXjIqrb08S-42w.jpeg" /><figcaption>Because who doesn’t like cookies?</figcaption></figure><p><strong>The Event Handler</strong></p><p>The event handler, well, handles the event, whatever it is. It could be a click, a mouseover, a hover, whatever you want the user to do to trigger your change in state. In the cookie jar analogy, this would be the action of unlocking/opening the cookie jar so you can put in or take out cookies (but the actual event is the click, as you can see in the <strong><em>onClick</em></strong> attribute for the &lt;button&gt; element).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/435/1*6-jQe4UETdJgLEibPBU0XA.jpeg" /><figcaption>Remember to wrap your Javascript stuff in curly braces in the render statement!</figcaption></figure><p>The <strong><em>handleClick() </em></strong>method is our example’s event handler. You’ll notice that the setState() method is inside of it. This is standard, so just get used to putting setState() inside of a method every time. Drilling this syntax is going to help this become muscle memory for you, so I recommend typing it several times.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*WWBJouTOfVnWSV_mWu2s1w.jpeg" /></figure><p>This method-within-a-method thing is just a way for React to protect state from accidental direct changes. It’s why you can’t just assign a new object to state with the assignment operator (=), and it’s a good thing. It may be roundabout, but it’s best practice and part of functional programming principles to not alter state directly.</p><p><strong>Binding Statement</strong></p><p>Don’t forget the binding statement! This is what <em>binds</em> your <strong><em>handleClick()</em></strong> method to your CookieJar component. Why do we have to bind it? Because it lets you use the method anywhere in the component. In the cookie jar analogy, it’s like getting permission to open the cookie jar in the first place. Without the binding statement, you’re not going to be able to get into that jar, no matter how good those cookies look (maybe using this example was a mistake, I’m getting hungry).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*UG_WqLo5ILFckUuGuFNS6Q.jpeg" /><figcaption>‘this’ just refers to the CookieJar component’s scope.</figcaption></figure><p>Just make sure you put the binding statement in the constructor function (usually right after you initialize state), and put the method definition between the constructor function and the render method. Location matters.</p><p><strong>setState() Method</strong></p><p>This is where the magic happens. Maybe you baked a fresh batch of cookies and need to add them to the jar, or maybe you’re going to sneak one or two in the middle of the night while your roommate’s asleep. Either way, this is where you decide how state is going to change.</p><p>In our example, we just baked a pan of 12 snickerdoodles, and we want to add them to the jar every time we click that button (see the render() method in the full example).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/1*A7IQZH_9sMnE9tcpAaC8MQ.jpeg" /><figcaption>Cookies and React: a match made in oven.</figcaption></figure><p>In order to know how many snickerdoodles we’ll have once the new ones are added, we need to know how many snickerdoodles were already in the jar to begin with. That’s where the next topic comes in.</p><p><strong>Current/Previous State Object Parameter</strong></p><p>We need to know what the cookie jar looks like right now (present) before we change it (future). Or we can put it another way, that we need to know what it LOOKED like (past) before we MADE changes to it (present). Either way, the basic idea is the same: state before and state after. This parameter <em>oldCookies</em> is what we’ll call <strong>the before</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/282/1*GSQ51x9Sqwii8-PUVmqWIw.jpeg" /><figcaption>This picture was taken in 2019 B.C. (before cookies)</figcaption></figure><p>You might see this parameter named <em>prevState</em> in other people’s code, and that’s fine. Just remember it’s important to include this parameter in your setState() method because without it, you’re throwing cookies into the jar blind. More importantly, React won’t let you change anything without this parameter because it wants to refer to the old state’s properties to form the new state.</p><p><strong>Return statement in setState()</strong></p><p>This is the last bit that I know I had trouble understanding at first. Think of it as <strong>the after</strong>. You’ve referred to <strong>the before</strong> already, and now you’re defining the appearance of state after your changes have been made. It’s the new state object, but you don’t have to list every property from the old object, because React keeps those the same for you unless you change them here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/1*HBUs32NZ-yVJbOyrwgXQyg.jpeg" /></figure><p>We only baked snickerdoodles this time, so we’re only going to add 12 to snickerdoodles. The rest of the cookies are still in the jar. We’re just not modifying them, so they don’t need to be mentioned in the <strong>return statement</strong>.</p><p><strong>Recap</strong></p><p><em>Event handler</em> — unlocks the cookie jar (accesses state)</p><p><em>Binding statement</em> — gives you permission to open the cookie jar (allows access to state)</p><p><em>setState() method</em> — changes what’s in the cookie jar (updates state based on changes you make in the return statement)</p><p><em>Current/Previous state object parameter</em> — the cookie jar before you make your changes (state before your changes are made; you have to refer to it to make the new state)</p><p><em>Return statement in setState()</em> — the cookies you’re adding, removing, or otherwise changing (the new look of the state object; your changes to state)</p><p>Let me know if this analogy helped in the comments below! Thanks for reading ^_^</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49102dcfd25" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>