<?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 Keshav Goyal on Medium]]></title>
        <description><![CDATA[Stories by Keshav Goyal on Medium]]></description>
        <link>https://medium.com/@keshavgoyal2014?source=rss-9324cebd26b4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_WXx6uaDUBoneo8FsIa9KA.jpeg</url>
            <title>Stories by Keshav Goyal on Medium</title>
            <link>https://medium.com/@keshavgoyal2014?source=rss-9324cebd26b4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 23:08:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@keshavgoyal2014/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[State and Props in ReactJS]]></title>
            <link>https://medium.com/@keshavgoyal2014/state-and-props-in-reactjs-7bb7a57c0dfd?source=rss-9324cebd26b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/7bb7a57c0dfd</guid>
            <category><![CDATA[setstate]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[props-and-state]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Keshav Goyal]]></dc:creator>
            <pubDate>Mon, 01 Jun 2020 17:22:29 GMT</pubDate>
            <atom:updated>2020-06-01T18:58:44.207Z</atom:updated>
            <content:encoded><![CDATA[<p>While working with ReactJS, you come across the terms <strong>State</strong> and <strong>Props</strong> very frequently. Understanding these terms is a big step towards learning ReactJS.</p><p>In this article, I will discuss State and Props and talk about some of the common questions related to State and Props.</p><h3>What are Props?</h3><p><strong>Props</strong> are used to pass data from the parent component to the child component. In ReactJS, the data flow is <strong>unidirectional</strong> from parent to child.</p><h3>How to pass data with props?</h3><p>Now let’s see an example of passing data with props.consider two components 1.Parent 2. Child</p><pre>class Parent extends React.Component{<br>      render()<br>      {   <br>         return(<br>            &lt;Child data=&quot;props-explanation&quot; /&gt;<br>            )<br>       }<br>}</pre><p>This is the parent component that passes the data <strong><em>“props-explanation”</em></strong> to its child component through prop named <strong><em>data</em></strong>.</p><pre>class Child extends React.Component{<br>      constructor(props)<br>      {<br>         super(props)<br>      }<br>      render()<br>      {   <br>         return(<br>            &lt;h1&gt;{this.props.data}&lt;/h1&gt;<br>            )<br>      }<br>}</pre><p>The constructor for a React component is called before it is mounted. While implementing the constructor for this subclass, you should call super(props) before any other statement; otherwise, this.props will be undefined. this.props is an object which stores all the Props of the component. So this.props.data gives the value of data passed from the parent component to the child component.</p><h3>Props are Read-Only</h3><p>A component must never update its own props. A component that uses only Props is pure that means given the same input, it will provide the same output.</p><h3>What is State?</h3><p>Similar to <strong>Props, State</strong> also holds information about the component. But unlike props, components cannot pass data with State. The State is entirely private to the component and entirelycontrolled by the component.</p><h3>How To Use State?</h3><p>Let’s walk through an example of how to use State in a component.</p><pre>class State_example extends React.Component{<br>      constructor(props)<br>      {<br>         super(props)<br>         this.state={<br>            cost:100,<br>            name:&#39;book&#39;<br>         }<br>      }<br>      render()<br>      {   <br>         return(<br>            &lt;h1&gt;{this.state.cost}&lt;/h1&gt;<br>            )<br>      }<br>}</pre><p>In the class constructor, this.state is initialized, and then this.state can be used similar to this.props .</p><h3>Updating State</h3><p>State can be updated by using the method this.setState() . On triggering this.setState() React re-renders the component and the child components based on the changes to the State. this.state should not be modified directly as this will not re-render the component.</p><p>Example of using this.setState()</p><pre>this.setState({cost:200})</pre><p>This will update the value of cost to 200 and re-render the component.<strong>State updates are merged</strong> when you call this.setState() React merges the object you provided into the current state.</p><h3><strong>State updates may be Asynchronous.</strong></h3><p>As updates to state may be asynchronous you should not rely on this.state or this.props value for updating the state.</p><pre>this.setState({cost:this.state.cost+100})    //wrong</pre><p>To fix this we use the other form of this.setState which accepts a function rather than object. The first parameter to the function is the <strong>state</strong> of the component at the the time of triggering this.setState and the second parameter is <strong>props</strong> of the component.</p><pre>this.setState((prev_state,props)=&gt;{<br>      return(<br>        {cost:prev_state.cost+100}<br>       )<br>      }<br>)</pre><p>The output of this function is merged with the <strong>state</strong> of the component.</p><p>The second parameter to this.setState is a callback function that is executed once this.setState is completed, and the component is re-rendered.</p><h3><strong>Updating State from Child Component</strong></h3><p>As <strong>State</strong> is private to a component, one issue which you may face is how to update State from the child component.</p><p>This can be solved by passing this.setState as props to the child component.</p><p>Now let’s see this in use with two components 1. Parent 2. Child</p><pre>class Parent extends React.Component<br>{<br>    constructor(props)<br>    {<br>        super(props)<br>        this.state={books:[{cost:100,name:&#39;Learn ReactJS&#39;},                                                                    {cost:500,name:&#39;Learn JavaScript&#39;}]<br>             }<br>    } </pre><pre>updatecost =(i)=&gt;{<br>      this.setState((prev_state)=&gt;{<br>           return(<br>               {<br>                 books:prev_state.books.map((book,index)=&gt;{<br>                             if(index===i)<br>                             {<br>                                 return(<br>                                   {<br>                                      ...book,<br>                                      cost:book.cost+100<br>                                   })<br>                              }<br>                              else<br>                              {<br>                                 return(<br>                                   {<br>                                      ...book<br>                                   })<br>                              }<br>                    })<br>                })<br>           })<br> }</pre><pre>render()<br>{<br>    return(<br>         &lt;div&gt;{this.state.books.map((book,index)=&gt;{<br>             return(<br>               &lt;Child name={book.name} cost={book.cost} updatecost=              {()=&gt;{this.updatecost(index)}}/&gt;<br>                   )<br>          })}&lt;/div&gt;<br>            )<br>}</pre><pre>}</pre><p>Component Parent contains an array of books in the state, and here I used the map method for the array elements to go through every element of the array and pass them to Child component as props. Here the prop with name <em>updatecost</em> passes the function, which updates the state when the corresponding event occurs in the Child Component.</p><pre>class Child extends React.Component<br>{<br>    constructor(props)<br>    {<br>        super(props)<br>    }<br>    render()<br>    {<br>       return(<br>        &lt;div style={{display:&#39;flex&#39;,alignItems:&#39;center&#39;,<br>              justifyContent:&#39;center&#39;,  flexDirection:&#39;column&#39;}}&gt;</pre><pre>        &lt;h3 style={{paddingRight:&#39;20px&#39;}}&gt;{this.props.name}&lt;/h3&gt;</pre><pre>         &lt;p&gt;Cost is:{this.props.cost}&lt;/p&gt;<br>      &lt;button onClick={this.props.updatecost}&gt;Increase cost&lt;/button&gt;<br>      &lt;/div&gt;<br>       )<br>    }</pre><pre>}</pre><p><strong>The button in the child component triggers the updatecost function, which updates the state of parent function and re-renders the parent and child component.</strong></p><p>So, passing setState through props to the child component, lets us update the state value of Parent Component from Child Component.</p><p>You can find the code for above example <a href="https://codepen.io/keshav2807/project/editor/Xpwxbd">here</a>.</p><h4>Let’s Finally recap some of the important points regarding State and Props.</h4><ol><li>Props are <strong>Read Only.</strong></li><li>State is completely <strong>Private</strong> to a Component.</li><li>State Updates may be <strong>Asynchronous.</strong></li><li>Updating State of Parent Component form Child Component.</li></ol><p>I hope this article helps you understand props and state in ReactJS.If you have any doubts feel free to ask in the responses. I would be happy to help.</p><p>There are also other important things in React about which I will keep writing in my following articles.</p><p>You can also read about <a href="https://medium.com/@keshavgoyal2014/introduction-to-react-js-a2f3f4bbbe3e">Introduction to ReactJS</a> .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y6C4nSvy2Woe0m7bWEn4BA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7bb7a57c0dfd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction To React.JS]]></title>
            <link>https://medium.com/@keshavgoyal2014/introduction-to-react-js-a2f3f4bbbe3e?source=rss-9324cebd26b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2f3f4bbbe3e</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[introduction]]></category>
            <category><![CDATA[basics]]></category>
            <dc:creator><![CDATA[Keshav Goyal]]></dc:creator>
            <pubDate>Sat, 23 May 2020 18:25:18 GMT</pubDate>
            <atom:updated>2020-05-24T05:15:18.684Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction To ReactJS</h3><p>You must have heard about ReactJS, a JavaScript library for building user interfaces and web applications. Some of the websites created with ReactJS are Facebook, Airbnb, Netflix, and many more. Feeling Excited, let’s dive in for more details.</p><figure><img alt="logo of reactjs" src="https://cdn-images-1.medium.com/max/1024/1*y6C4nSvy2Woe0m7bWEn4BA.png" /><figcaption>taken from reactjs.org</figcaption></figure><h3>Why ReactJS?</h3><p>The first thought you may encounter while hearing about ReactJS is why to learn ReactJS.</p><p><strong>The Reasons that make ReactJS Special are:</strong></p><ol><li><em>ReactJS uses </em><a href="https://en.wikipedia.org/wiki/Function_composition_(computer_science)"><em>Composition</em></a><em>.</em></li><li><em>ReactJS is Declarative.</em></li><li><em>ReactJS has Unidirectional Data Flow.</em></li></ol><p>The above reasons make ReactJS a preferred choice for a frontend developer.</p><h3>Getting Started with ReactJS</h3><p>Make sure that you have the latest version of<strong> </strong><a href="https://nodejs.org/en/download/"><strong>NodeJS</strong></a> and <a href="https://www.npmjs.com/"><strong>npm</strong></a> installed. We will be using create-react-app package to set up the react project.</p><p>For installing <strong>create-react-app</strong> move to the terminal and type</p><pre>npm i create-react-app -g</pre><p>This installs <strong>create-react-app</strong> package globally in your system. Now we can use the create-react-app package to get started with ReactJS project.</p><pre>create-react-app new_project</pre><p>This will create a new folder new_project with some files in it(it will take some minutes to create). Open it with a text editor, and you will see the folder structure like this.</p><figure><img alt="Folder structure of new_project" src="https://cdn-images-1.medium.com/max/439/1*030nIy8ATakuRt0Qz9gq_g.png" /></figure><p>Now to run the app</p><pre>cd new_project<br>npm start</pre><p>Now the app will be running on localhost:3000 you will see the page looks like this.</p><figure><img alt="home of app created by create-react-app" src="https://cdn-images-1.medium.com/max/1024/1*4TeLxWw4LHQuTOR8A0eT2Q.png" /></figure><p>Now you have successfully created and ran a <strong>ReactJS</strong> App.</p><h3>Now let’s see some details about the folder structure:</h3><ol><li>public/index.html is the page template</li><li>src/index.js is the JavaScript entry point.</li></ol><p>These files must exist with exact filenames. You can rename or delete other files. You can read more about these in the <a href="https://create-react-app.dev/docs/folder-structure/">docs</a>.</p><h3>Now let’s make some changes to the App so that it displays your name.</h3><ol><li>Open the file App.js in src directory.</li><li>make changes to the App.js file so that it same as the below.</li></ol><figure><img alt="make the App.js file same as this image remove all the code inside the div with classname App and import statement of logo" src="https://cdn-images-1.medium.com/max/1024/1*9nIjeNu32iPmOhDRKCQ7FA.png" /></figure><p>3. Add an paragraph tag with your name in the &lt;div&gt; .</p><p>4. Now the Home page of the app will look like this the details will change according to the HTML code you have used.</p><figure><img alt="The app will look like this after the changes" src="https://cdn-images-1.medium.com/max/963/1*VmO_e3zRl51TAwkEUIFP2g.png" /></figure><p>Congratulations 👏 👏 You have successfully created a react app and ran it and made some changes to it.</p><p>For further reading you can always refer the <a href="https://reactjs.org/">docs</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2f3f4bbbe3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>