<?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: Shubham Kant</title>
    <description>The latest articles on DEV Community by Shubham Kant (@shubhamkant_).</description>
    <link>https://dev.to/shubhamkant_</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%2F1142073%2F284940b3-2508-45ef-ac91-648a738c4f88.jpeg</url>
      <title>DEV Community: Shubham Kant</title>
      <link>https://dev.to/shubhamkant_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shubhamkant_"/>
    <language>en</language>
    <item>
      <title>Why we need State variables in react</title>
      <dc:creator>Shubham Kant</dc:creator>
      <pubDate>Mon, 28 Aug 2023 10:45:04 +0000</pubDate>
      <link>https://dev.to/shubhamkant_/why-we-need-state-variables-in-react-4lb1</link>
      <guid>https://dev.to/shubhamkant_/why-we-need-state-variables-in-react-4lb1</guid>
      <description>&lt;p&gt;So I got stuck on a very interesting problem today.&lt;br&gt;
Then got to know about &lt;code&gt;state variables&lt;/code&gt; in react. So I thought I should share what I learnt today.&lt;/p&gt;

&lt;p&gt;We will learn about :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What are State variables ?&lt;/li&gt;
&lt;li&gt;How we can make a state variable ?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why we need state variables ? (mainly we will explore this!!!)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  What are State variables ?
&lt;/h2&gt;

&lt;p&gt;State variables maintain the state of your component.&lt;/p&gt;

&lt;p&gt;We can make state variables via useState() Hook. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hooks are JavaScript functions given by React. Period!!!&lt;br&gt;
React has some hooks. Each has some superpowers (logic is written behind the scene).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  How we can make a state variable ?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;First, You will need to import useState() hook like a named import - &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;import { useState } from "react";&lt;/code&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialize useState&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;const [A,setA] = useState('Hello world');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;useState() hook returns an array with 2 values.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;variable which have the respective value(&lt;code&gt;Hello world&lt;/code&gt; in above case)&lt;/li&gt;
&lt;li&gt;The function to update the value&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now our state variable &lt;code&gt;A&lt;/code&gt; has &lt;code&gt;Hello world&lt;/code&gt; value and via &lt;code&gt;setA()&lt;/code&gt; method we can change the value &lt;code&gt;A&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;em&gt;How to change the value via above method ?&lt;/em&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Simply pass the updated value in function.&lt;br&gt;
&lt;code&gt;setA('Bye world');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the value of &lt;code&gt;A&lt;/code&gt; is being updated to &lt;code&gt;Bye world&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why we need state variables ?
&lt;/h2&gt;

&lt;p&gt;Let's understand with an example here - &lt;/p&gt;

&lt;p&gt;We need to add a toggle functionality i.e. feature of Login/Logout button, If a user is logged out ,then button text should be &lt;code&gt;Login&lt;/code&gt; and if click on it, text should change to &lt;code&gt;Logout&lt;/code&gt; and vice versa.&lt;/p&gt;

&lt;p&gt;It sounds easy right!!&lt;br&gt;
We can make a variable and store a value in it and on click of the button, change the text.&lt;/p&gt;

&lt;p&gt;In below code snippet -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We made a variable &lt;code&gt;SessionStatus&lt;/code&gt; and its initial value is &lt;code&gt;Login&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We added a &lt;code&gt;onClick&lt;/code&gt; handler to change the text accordingly.&lt;/li&gt;
&lt;li&gt;Added a simple logic to change the button text to &lt;code&gt;Logout&lt;/code&gt; if its value is &lt;code&gt;Login&lt;/code&gt; and vice versa.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also added a log statement to check if our onClick handler is getting called and our variable is getting updated or not.&lt;br&gt;
Please try - &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code snippet 1&lt;/strong&gt;&lt;br&gt;
&lt;iframe src="https://stackblitz.com/edit/react-q1w9aq?file=src%2FApp.js" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;If you will see in the console, the variable value is changing that means our click Handler is getting called but we are &lt;code&gt;NOT&lt;/code&gt; able to see the change in button text in our UI.&lt;br&gt;
It's really weird!!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Let's try making &lt;code&gt;SessionStatus&lt;/code&gt; a state variable and try the same&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code snippet 2&lt;/strong&gt;&lt;br&gt;
&lt;iframe src="https://stackblitz.com/edit/react-p7mt1t?file=src%2FApp.js" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Here it is working. Text of button is &lt;code&gt;ALSO&lt;/code&gt; changing in UI. &lt;strong&gt;&lt;em&gt;But why ???&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State variables syncs UI layer with data layer. Period!!!&lt;/strong&gt;&lt;br&gt;
They maintain the state of your component.&lt;/p&gt;

&lt;p&gt;Here the value of &lt;code&gt;state variable&lt;/code&gt; is changing and &lt;strong&gt;when ever state variables changes/updates, React re-renders the whole component&lt;/strong&gt; or we can say react reloads the whole component or we can say react calls the component again with the updated values this time.&lt;/p&gt;

&lt;p&gt;React does it via &lt;strong&gt;Diff Algorithm&lt;/strong&gt;, i.e. calculates the difference between &lt;code&gt;previous Virtual DOM&lt;/code&gt; and &lt;code&gt;new Virtual DOM&lt;/code&gt; and then that difference is applied to Actual DOM.&lt;br&gt;
Here only button value is changing, so react re-renders the component again with that change only in the DOM.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Virtual DOM&lt;/strong&gt; - React keeps a track of UI (the DOM) in memory in the form of object and it is Virtual DOM.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;React makes the &lt;code&gt;DOM operations&lt;/code&gt; superfast.&lt;br&gt;
This is the core of react.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is an interesting observation if you want to understand it one level deep -&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You will observe something weird in code snippet 2. &lt;/p&gt;

&lt;p&gt;Let's say the first time, the text in the button is currently is &lt;code&gt;Login&lt;/code&gt; and on click, it changes to &lt;code&gt;Logout&lt;/code&gt; but in console it's value is still printing as &lt;code&gt;Login&lt;/code&gt;. &lt;br&gt;
&lt;strong&gt;&lt;em&gt;Why is that ??&lt;/em&gt;&lt;/strong&gt; &lt;br&gt;
&lt;strong&gt;&lt;em&gt;We put console.log() after value being updated right ??&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Then why it does not printed the updated value then and there ??&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The answer to this is that &lt;code&gt;Setting it&lt;/code&gt; does not change the state variable you already have, but instead &lt;code&gt;triggers a re-render&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;React will make a &lt;code&gt;new instance&lt;/code&gt; of state variable with the &lt;code&gt;new value&lt;/code&gt; in a &lt;code&gt;new render cycle&lt;/code&gt;. &lt;strong&gt;Period!!!&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The value which is printing on clicking the button is the value of the variable in the component before the re-render triggered.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Got any doubt or wanna chat? Reach out to me on &lt;a href="https://www.linkedin.com/in/shubham-kant/"&gt;linkedIn&lt;/a&gt; or &lt;a href="https://twitter.com/shubhamkant_"&gt;twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>frontend</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
