<?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 Siva Kumar on Medium]]></title>
        <description><![CDATA[Stories by Siva Kumar on Medium]]></description>
        <link>https://medium.com/@sivakumar_93694?source=rss-6e3596d2ba6b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*wFXy5aXgRhHPVS3f.</url>
            <title>Stories by Siva Kumar on Medium</title>
            <link>https://medium.com/@sivakumar_93694?source=rss-6e3596d2ba6b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 05:13:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sivakumar_93694/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[Why not to mutate React state?]]></title>
            <link>https://medium.com/@sivakumar_93694/why-not-to-mutate-react-state-34be55a90799?source=rss-6e3596d2ba6b------2</link>
            <guid isPermaLink="false">https://medium.com/p/34be55a90799</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[purecomponent]]></category>
            <dc:creator><![CDATA[Siva Kumar]]></dc:creator>
            <pubDate>Mon, 30 Sep 2019 09:43:27 GMT</pubDate>
            <atom:updated>2019-09-30T09:43:27.764Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*agmQB9mz2Ma_iKpeyBleew.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@lukechesser?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Luke Chesser</a> on <a href="https://unsplash.com/s/photos/react-js?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Have you ever cursed the React for not rerendering your component? If so, mutating the state might be one of the reasons for not rerendering your component.</p><p>Let’s consider a simple component which renders the list of countries from the state. We add a button to add country to state on click. To update the list in the state, there are two ways.</p><p>The below method will add a country to the state on every call, but it mutated the list. <strong>By mutation, I mean changing or affecting a source element</strong>.</p><pre>addCountryWithMutation = () =&gt; {<br>    const { countries } = this.state;<br>    countries.push(faker.address.country());<br>    <br>    this.setState({ countries }, () =&gt; {<br>       console.log(&quot;countries added by addCountryWithMutation&quot;, this.state.countries);<br>    });<br>}</pre><p>The other way is to clone the list and then append a new entry.</p><pre>addCountryWithoutMutation = () =&gt; {<br>    const { countries } = this.state;<br>    <br>    this.setState({<br>      countries: [...countries, faker.address.country()]<br>    }, () =&gt; {<br>       console.log(&quot;countries added by addCountryWithoutMutation&quot;, this.state.countries);<br>    });<br>}</pre><p>In both the methods, if you check the console you can see the countries list will be updated with a new entry. But, the component won’t be rerendered when you add the country via add country with mutation button. But if you click add country without mutation button, now the component render will be called and the list of countries will be displayed.</p><p>You can check the behavior in action as well.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fvickysiva%2Fembed%2Fpreview%2FoNvRRwB%3Fheight%3D600%26slug-hash%3DoNvRRwB%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;url=https%3A%2F%2Fcodepen.io%2Fvickysiva%2Ffull%2FoNvRRwB&amp;image=https%3A%2F%2Fscreenshot.codepen.io%2F859201.oNvRRwB.small.d6e2fe4a-02b3-4f19-9def-3d17ca6a6853.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/12b1fe0e0d3b94b6a64b1c5d21fb414e/href">https://medium.com/media/12b1fe0e0d3b94b6a64b1c5d21fb414e/href</a></iframe><p>This is because, PureComponent will do shallow check for props and state. Since we are mutating the state, both props will be the same and hence the rerender won’t be called.</p><p>Note: This behavior, won’t happen when we extend React.Component as there will be no check for props and state.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34be55a90799" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>