<?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 Dhinesh Rajendran on Medium]]></title>
        <description><![CDATA[Stories by Dhinesh Rajendran on Medium]]></description>
        <link>https://medium.com/@dhineshrajendran?source=rss-7d12d744e810------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*eitxxRIp-mZF2QnqzGDlqg.jpeg</url>
            <title>Stories by Dhinesh Rajendran on Medium</title>
            <link>https://medium.com/@dhineshrajendran?source=rss-7d12d744e810------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 07:39:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhineshrajendran/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[Complete guide about UseState Hook in React]]></title>
            <link>https://medium.com/@dhineshrajendran/complete-guide-about-usestate-hook-in-react-88cf1d4bd5?source=rss-7d12d744e810------2</link>
            <guid isPermaLink="false">https://medium.com/p/88cf1d4bd5</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[usestate-hook]]></category>
            <category><![CDATA[hooks]]></category>
            <dc:creator><![CDATA[Dhinesh Rajendran]]></dc:creator>
            <pubDate>Wed, 05 Jan 2022 06:59:59 GMT</pubDate>
            <atom:updated>2022-01-05T10:44:28.652Z</atom:updated>
            <content:encoded><![CDATA[<h3>UseState Hook in React</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vsGKw42WUtnBrzFeHbpPmg.png" /></figure><h3>Introduction</h3><p>Hey,</p><p><strong><em>UseState </em></strong>is a method that<strong><em> </em></strong>allows you to maintain the local state in the component. It has a very simpler syntax. We can update the state through only one function. So, we can overcome a lot of unwanted bugs and data mutating issues.</p><p>So in this blog, I almost cover how to use, good and bad practices with small examples.<br>Let’s get going.</p><h3>What is Hooks?</h3><p>We could say, Hooks are utility functions. Using this, we can write clean and maintainable code. This makes developer life easier.</p><p><em>Note: React Hooks can be used only on functional components. <br>Nevertheless, class components have their own way to maintain the state.</em></p><h3>What is useState?</h3><p>useState() is a method to maintain the Local state in the component.<br>The state value is empty by default.</p><p>We can initialize the state using the useState method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*P-PcoteG7u7CHhBja5jg4w.png" /></figure><h3>Why can we use this?</h3><ol><li>The state is always immutable. While updating the state, behind the scenes, it will create a new reference(instead, It doesn’t mutate with the existing object) and assign it to the current state variable.</li><li>Whenever the state changes, the component will be re-rendered. So we can update the latest data in the view. but if we are not using the state, the component will be triggered once(Initial render). So using this way, it is really difficult to update the view.</li></ol><h3>How to use it?</h3><p>Before using the state, We can import the useState method from ‘react’.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*D7HMPIcKZZDTzk7y4gk5-A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Og_srek--hCXhSZSKoOXLQ.png" /></figure><p>The useState method returns an array of two elements(<em>[profile, updateProfileState]</em>). Here, the <strong><em>profile </em></strong>would be used to refer to the current state(That always holds the current state value), and <strong><em>updateProfileState </em></strong>is a function to update the state.</p><p>We have a small JSX expression to show the name, email, and update button in UI.<br>Basically, our goal is to update the email into the state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*Z4cOLvYPqjryl6-9cZmKcQ.png" /></figure><p>We’re using a small function (updateEmail) to update the state. <br>While updating the state, we have to use the previous state value(prevState) and the current property value(email).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*iB_ieOfH2nYlHnY4Es4CLQ.png" /></figure><p>If we are not using the previous state, we can lose some properties from the profile object. That is not a good practice also.</p><p>We can use the useState method more than once to maintain the different states in a component as mentioned below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/769/1*hxgSVNw-vkmFkgbTdTuOjg.png" /></figure><p>Then, If we want to manage the single state for both parent and child components, that is also possible. But If we want to update the state, we have to send the callback to the parent component and update the state.</p><h3>Good and bad practices</h3><p>useState method should be called only on the top level of react function.<br>So useState method will be executed in the same order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NV8Joy7Fj-m-Y1-zpkDtLg.png" /></figure><p><em>Note: Don’t declare a hook inside an if condition, for loops, and, inside a normal javascript function. Basically, it will throw an error while compiling the code.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3fPteBjJYdECL0u7xfe9uw.png" /></figure><p>React provides two ways to initialize the state.</p><p><em>Mostly we can use this way to initialize the state. We can go with this approach if we are initializing static numbers or strings. if we have objects or arrays just go with the function version of initializing state. The reason I&#39;ve mentioned below(2nd point)</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*P-PcoteG7u7CHhBja5jg4w.png" /></figure><p><em>The second way is to be, We’ve to use the function version to initialize the state. The main idea to go with this approach is that, whenever the state changes, the component will be re-rendered again.</em></p><p><em>At the same time, the useState method is also triggered while state changes. if we are computing a complex logic, it might have taken some time to finish the execution. So every state changes, our complex logic will be executed. So it leads to performance issues.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*Tbq1PSPjMh5DqvFgMpQ_Aw.png" /></figure><p>Finally, I&#39;ve attached the sample code which I worked with the useState hook.</p><p><a href="https://stackblitz.com/edit/react-bp6qwr?file=src/App.js">React - UseState Hook - StackBlitz</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88cf1d4bd5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>