<?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 M Rakshit on Medium]]></title>
        <description><![CDATA[Stories by M Rakshit on Medium]]></description>
        <link>https://medium.com/@mrksht?source=rss-7d33a160d681------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*LkxH3VvjHJul4_JJ</url>
            <title>Stories by M Rakshit on Medium</title>
            <link>https://medium.com/@mrksht?source=rss-7d33a160d681------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 06:49:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mrksht/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[A simple React Todo list.]]></title>
            <link>https://medium.com/@mrksht/a-simple-react-todo-list-13b779a449bf?source=rss-7d33a160d681------2</link>
            <guid isPermaLink="false">https://medium.com/p/13b779a449bf</guid>
            <category><![CDATA[to-do-list]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[to-do-list-app]]></category>
            <category><![CDATA[todo]]></category>
            <category><![CDATA[react-js-tutorials]]></category>
            <dc:creator><![CDATA[M Rakshit]]></dc:creator>
            <pubDate>Thu, 30 Apr 2020 14:23:15 GMT</pubDate>
            <atom:updated>2020-04-30T15:09:09.258Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*CtHRfxjyXRAADIS8gue9nw.png" /></figure><p>In the previous article I have discussed about the most basic things which we will be using quite regularly in React. If you have not read it you can find it <a href="https://medium.com/swlh/introduction-to-react-d3c0a6c891a8">here</a>.</p><p>In this article we are going to build a simple Todo list using React. I wouldn’t focus much on the CSS part but at the end of this article I would like show you how to write a CSS file in React so that you can try and make your app look much beautiful. It’s quite easy actually :)</p><p><strong>In our Todo list, we would like to do two things:</strong></p><ol><li>Add a Todo item</li><li>Delete a Todo item</li></ol><p>When ever you build a React application, I would like you to have a clear idea about how do you want to divide your <strong>App Components. </strong>In this particular application I want to have two components, one for the App Header or Navbar, and the second one is for the main Todo list. It is a good habit to divide your code component wise because it doesn’t make your code base look clumsy and it is easy to find the broken pieces of code when there is modularity. Another reason for this is that the fun of React lies in how do you play with the components.</p><h4>How do we build the App?</h4><p>Firstly, let us start by creating the React boilerplate code which can be created using the <strong>create-react-app</strong> package. I have discussed this in the previous article very clearly. <strong>(Link given at the beginning)</strong></p><pre>create-react-app todo</pre><pre>cd todo<br>npm start</pre><p>Then you can open your browser and go to <a href="http://localhost:3000/">http://localhost:3000/</a> to see your React app running.</p><p>If you open the project folder in an IDE. The structure of the project looks like this:</p><pre>todo/<br>  README.md<br>  node_modules/<br>  package.json<br>  .gitignore<br>  public/<br>    favicon.ico<br>    index.html<br>  src/<br>    App.css<br>    App.js<br>    App.test.js<br>    index.css<br>    index.js<br>    logo.svg</pre><p>Inside the <strong>src </strong>folder, let us first make a <strong>components</strong> folder where we create all our components.</p><p>We will then make our first <strong>Functional</strong> or <strong>Class</strong> component, <strong>Header.js </strong>where we will write the code for the Header or the Navbar for the application. In this article I wouldn’t really want to make a full fledged Header component. I would like you all to give it a try and make it the way you want.</p><p>Note that I have mentioned that the <strong>Header.js</strong> can be a <strong>Functional</strong> or a <strong>Class </strong>component. The reason for that is the Header.js file does not have to deal with any variables which change their state over the period of time. So, it doesn’t really matter if we have it as a Functional or a Class component.</p><p><strong>The contents of Header.js file will look like:</strong></p><pre>import React, { Component } from &#39;react&#39;</pre><pre>class Header extends Component {<br>  render() {<br>    return (<br>      &lt;div&gt;<br>        &lt;h3&gt;React Todo List&lt;/h3&gt;<br>      &lt;/div&gt;<br>    )<br>  }<br>}</pre><pre>export default Header</pre><p>To render the component in our browser, we need to import the Header component in the <strong>App.js</strong> file.</p><p><strong>The contents of the App.js file will look like:</strong></p><pre>import React from &#39;react&#39;;<br>import logo from &#39;./logo.svg&#39;;<br>import &#39;./App.css&#39;;<br>import Header from &#39;./components/Header&#39;</pre><pre>function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;Header /&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><pre>export default App;</pre><p><strong>The web-app in the browser looks like:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jW66-i-T5ZYzM7t6Vq7MoQ.png" /><figcaption>Header component</figcaption></figure><p>Now, let us create the <strong>Todo.js </strong>component inside the components folder. This component must be a state component because we will be dealing with state variables inside this.</p><h3>1. Todo Component with state variables</h3><p><strong>The contents of Todo component initially look like:</strong></p><pre>import React, { Component } from &#39;react&#39;</pre><pre>class Todo extends Component {<br>  constructor(props) {<br>    super(props)<br>    this.state = {<br>      item: &#39;&#39;,<br>      todos: []<br>    }<br>  }</pre><pre>  render() {<br>    return (<br>     &lt;div&gt;</pre><pre>     &lt;/div&gt;<br>    )<br>  }<br>}</pre><pre>export default Todo</pre><p>You can see that we have declared two state variables, <strong>item</strong> and <strong>todos. </strong>The state variable <strong>item</strong> is for storing the value of the new Todo item which we are going to add. The state variable <strong>todos</strong> is actually a list that will store all the Todo list items.</p><h3>2. The Add Todo list form</h3><p>We need to create the form for adding a Todo item and also import it into the App.js to render the form in our browser.</p><p><strong>The contents of Todo.js look like:</strong></p><pre>import React, { Component } from &#39;react&#39;</pre><pre>class Todo extends Component {<br>  constructor(props) {<br>    super(props)<br>    this.state = {<br>      item: &#39;&#39;,<br>      todos: []<br>    }<br>  }</pre><pre>render() {<br>    return (<br>     &lt;div&gt;<br>       &lt;form&gt;<br>         &lt;input type=&quot;text&quot; onChange={this.handleChange} /&gt;<br>         &lt;button onClick={this.handleClick}&gt;Add&lt;/button&gt;<br>       &lt;/form&gt;<br>     &lt;/div&gt;<br>    )<br>  }<br>}</pre><pre>export default Todo</pre><p><strong>The contents of the App.js file look like:</strong></p><pre>import React from &#39;react&#39;;<br>import logo from &#39;./logo.svg&#39;;<br>import &#39;./App.css&#39;;<br>import Header from &#39;./components/Header&#39;<br>import Todo from &#39;./components/Todo&#39;</pre><pre>function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;Header /&gt;<br>      &lt;Todo /&gt;    <br>    &lt;/div&gt;<br>  );<br>}</pre><pre>export default App;</pre><p>The <strong>onChange</strong> and <strong>onClick </strong>are the event handlers. The onChange handler will get triggered whenever the value inside the input box changes and the onClick handler will be triggered whenever we click on the button.</p><p><strong>The app in our browser looks like:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zvF7v15WgWSjo_xvnIkNCA.png" /><figcaption>Todo form</figcaption></figure><h3>3. The event handlers</h3><p><strong>The contents of the Todo.js file look like:</strong></p><pre>import React, { Component } from &#39;react&#39;</pre><pre>class Todo extends Component {<br>  constructor(props) {<br>    super(props)<br>    this.state = {<br>      item: &#39;&#39;,<br>      todos: []<br>    }<br>  }</pre><pre>handleChange = (event) =&gt; {<br>  this.setState({<br>    item: event.target.value<br>  })<br>}</pre><pre>handleClick = () =&gt; {<br>  const list= this.state.todos<br>  todos.push(this.state.item)<br>    this.setState({<br>      todos: list<br>    })<br>}</pre><pre>render() {<br>    return (<br>     &lt;div&gt;<br>       &lt;form&gt;<br>         &lt;input type=&quot;text&quot; onChange={this.handleChange} /&gt;<br>         &lt;button type=&quot;button&quot; onClick= {this.handleClick}&gt;Add&lt;/button&gt;<br>       &lt;/form&gt;<br>     &lt;/div&gt;<br>    )<br>  }<br>}</pre><pre>export default Todo</pre><p>The App.js file looks the same.</p><p>The flow is somewhat like this, first you will type the todo item in the input text box. Each time when you enter a word or delete a word in the input text box, the value of the state variable <strong>item</strong> will change.</p><p>The <strong>handleChange </strong>method does the exact same thing described above. It will be triggered whenever the value inside the input text box is changed and will set the value of the state variable item as the present value inside the input text box.</p><p>Then, when we finish with typing the Todo item and click on the <strong>Add</strong> button, the value inside the item state variable will be pushed inside the <strong>list</strong> array and then the state of the <strong>todos</strong> array is set as <strong>list</strong>. This is taken care by the <strong>handleClick</strong> method. You must note that the button has an attribute type which is equal to button. It is a must to have this to avoid further complications because we are having this button inside the &lt;form&gt; tag and whenever we click the button the default nature of the form is to submit the form and not to do the actions which are given under the handleClick method. So, to avoid the form being submitted we must mention the type as button.</p><h3>4. Displaying the Todo list</h3><p><strong>The contents of Todo.js file look like:</strong></p><pre>import React, { Component } from &#39;react&#39;</pre><pre>class Todo extends Component {<br>  constructor(props) {<br>    super(props)<br>    this.state = {<br>      item: &#39;&#39;,<br>      todos: []<br>    }<br>  }</pre><pre>handleChange = () =&gt; {<br>  this.setState({<br>    item: event.target.value<br>  })<br>}</pre><pre>handleClick = () =&gt; {<br>  const list= this.state.todos<br>  todos.push(this.state.item)<br>    this.setState({<br>      todos: list<br>    })<br>}</pre><pre>render() {<br>    return (<br>     &lt;div&gt;<br>       &lt;form&gt;<br>         &lt;input type=&quot;text&quot; onChange={this.handleChange} /&gt;<br>         &lt;button type=&quot;button&quot; onClick={this.handleClick}&gt;Add&lt;/button&gt;<br>         &lt;ul&gt;<br>           {this.state.todos.map((todo, index) =&gt;<br>             &lt;span key={index}&gt;<br>               &lt;li&gt;{todo}&lt;/li&gt;<br>               &lt;button type=&quot;button&quot; onClick={() =&gt;                 {this.handleDelete(todo)}}&gt;delete&lt;/button&gt;<br>             &lt;/span&gt;<br>           )}<br>         &lt;/ul&gt;<br>       &lt;/form&gt;<br>     &lt;/div&gt;<br>    )<br>  }<br>}</pre><pre>export default Todo</pre><p>We will be using the <strong>map </strong>method of JavaScript to display the contents of the Todo list. We know that the contents of the list are inside the state variable <strong>todos. </strong>The map method will act as a loop which will go through all the values of the list which we are mapping on. Here, you can see that we passed two parameters <strong>todo</strong> and <strong>index. </strong>The todo parameter is the value of each iteration of the map and the index starts from 0 with 0 implying the index of the first item of the list.</p><p>It is a good habit to have a key attribute for every item of Todo list because it helps us differentiate all the items of the list.</p><p><strong>The app in our browser looks like:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/989/1*3s3bi-onTxVrlGfAfNyPsQ.png" /><figcaption>After adding two todo items</figcaption></figure><h3>5. Delete the list item</h3><p><strong>The contents of the Todo.js file look like:</strong></p><pre>import React, { Component } from &#39;react&#39;</pre><pre>class Todo extends Component {<br>  constructor(props) {<br>    super(props)<br>    this.state = {<br>      item: &#39;&#39;,<br>      todos: []<br>    }<br>  }</pre><pre> handleChange = () =&gt; {<br>   this.setState({<br>     item: event.target.value<br>   })<br> }</pre><pre> handleClick = () =&gt; {<br>   const list= this.state.todos<br>   todos.push(this.state.item)<br>     this.setState({<br>       todos: list<br>    })<br>  }</pre><pre>handleDelete = (todo) =&gt; {<br>  this.setState({<br>    todos: this.state.todos.filter(el =&gt; el !== todo)<br>  })<br>}</pre><pre>render() {<br>    return (<br>     &lt;div&gt;<br>       &lt;form&gt;<br>         &lt;input type=&quot;text&quot; onChange={this.handleChange} /&gt;<br>         &lt;button type=&quot;button&quot; onClick={this.handleClick}&gt;Add&lt;/button&gt;<br>         &lt;ul&gt;<br>           {this.state.todos.map((todo, index) =&gt;<br>             &lt;span key={index}&gt;<br>               &lt;li&gt;{todo}&lt;/li&gt;<br>               &lt;button type=&quot;button&quot; onClick={() =&gt;                 {this.handleDelete(todo)}}&gt;delete&lt;/button&gt;<br>             &lt;/span&gt;<br>           )}<br>         &lt;/ul&gt;<br>       &lt;/form&gt;<br>     &lt;/div&gt;<br>    )<br>  }<br>}</pre><pre>export default Todo</pre><p>We have another onClick handler on the button inside the list item. You can also have a look at how we pass the arguments to the method inside the event handler. Here also it is a must to mention the type of button as button because it is inside the &lt;form&gt; tag</p><p>We use the filter method of JavaScript which again runs the same way as the map. The logic in the handleDelete method is very simple. We are just filtering out the element whose value is same as that of the argument passed and returning the rest of the list which is set as the value of state variable todos.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/805/1*lPWAye8evR-Kz3dYtuRqGA.png" /><figcaption>After deleting the first Todo item</figcaption></figure><p>Here ends our simple Todo list app using React.</p><p>As I said at the beginning I would like to tell you how do we write CSS files in React.</p><p>There are actually three ways:</p><ol><li><strong>The inline CSS method:</strong></li></ol><p>This is the least recommended method of all. Here is an example of how to use it<br><strong>&lt;p style={{ “color”: “blue”, “width”: “20px” }}&gt;this is a paragraph&lt;/p&gt;<br></strong>It is a must to have all the values in double inverted commas and if there comes an attribute which is of two words, then we must write it in camel case format like<strong> “borderRadius”</strong>.</p><p><strong>2. Defining the CSS attributes inside a JavaScript variable:</strong></p><pre>const style = {<br>  height: &quot;20px&quot;,<br>  width: &quot;40px&quot;,<br>  borderRadius: &quot;2px&quot;<br>}</pre><pre>....</pre><pre>&lt;div style={style}&gt;This is div&lt;/div&gt;</pre><p><strong>3. Writing CSS in a file and importing:</strong></p><p>This is the most recommended way to write CSS with React. You can create a .css file somewhere in the project directory and then just import it into the component as import ‘path-to-the-css-file’. The rules of the CSS will be applied to all the elements of the component for which they are defined.</p><p><strong>Hope the article helps</strong></p><p>You can find the code for this on this <a href="https://github.com/rak-shit/React-Todo-Medium">repo</a></p><p>If you have any queries you can reach me <a href="https://www.linkedin.com/in/mrakshit">here</a>.</p><p>Enjoy reading :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13b779a449bf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to React]]></title>
            <link>https://medium.com/swlh/introduction-to-react-d3c0a6c891a8?source=rss-7d33a160d681------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3c0a6c891a8</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[basics-of-react]]></category>
            <category><![CDATA[introduction-to-react]]></category>
            <dc:creator><![CDATA[M Rakshit]]></dc:creator>
            <pubDate>Fri, 17 Apr 2020 16:08:41 GMT</pubDate>
            <atom:updated>2020-04-23T02:32:14.082Z</atom:updated>
            <content:encoded><![CDATA[<p>Firstly, let me tell you a story. When I started learning <strong>React </strong>I knew very little about web development. I did not even make a single website until that point and never knew how the client-side and server-side interact with each other. I used to be bewildered if someone used the word <strong>REST API. </strong>The reason I am saying all this is because you need not know any of the high-end stuff to learn <strong>React. </strong>All you need to know is very basic HTML, CSS and the basics of JavaScript.</p><h4>What is React?</h4><p><strong>React </strong>is an opensource JavaScript <strong>library </strong>that is used to build interfaces for single-page applications. React handles the view layer of the websites and mobile applications. React allows the developers to build large web applications which change data, without reloading the page. React also helps us in making reusable UI components. The main purpose of React is to make the web application fast, scalable and simple.</p><p>Before we start, we need to install two important things. We must install <strong>node</strong> in our system which we can get from <a href="https://nodejs.org/en/download/">https://nodejs.org/en/download/</a>. make sure you select the download file for the right Operating System.</p><p>After installing node, we need a package named <strong>create-react-app </strong>which will give us the boilerplate code needed for our React application. We will be editing the boilerplate code to get the desired outcome for our application.</p><p>To install <strong>create-react-app</strong> package :</p><pre>npm install -g create-react-app</pre><p>After installing the <strong>create-react-app </strong>package, we need to create a React project using it :</p><pre>create-react-app my-app</pre><pre>cd my-app/<br>npm start</pre><p>The above lines will create a React project and run it. We can our live React project on <a href="http://localhost:3000/">http://localhost:3000/</a> and it shows the below page :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7wnAPxN8xfls66QA8dlOeQ.png" /></figure><p>Now, have a look at the files that we created. It should look like this :</p><pre>my-app/<br>  README.md<br>  node_modules/<br>  package.json<br>  .gitignore<br>  public/<br>    favicon.ico<br>    index.html<br>  src/<br>    App.css<br>    App.js<br>    App.test.js<br>    index.css<br>    index.js<br>    logo.svg</pre><p>React is a huge library to explore. But, every application we build using React has to deal with <strong>components</strong>, <strong>props</strong>, <strong>states,</strong> and <strong>methods.</strong></p><h4>To completely understand this let us create few simple React web applications :</h4><p>The most important part of a React app are components. Components are the things we display in a React application. There are two types of components used in React</p><ol><li><strong>Functional Component </strong>or <strong>Stateless Component: </strong>A functional component is a simple JavaScript function that takes props as the argument and returns a React element. A React element is simple <strong>HTML</strong> or in the context of React, we call it <strong>JSX. </strong>It is also known as <strong>Stateless Component </strong>because you cannot change the state(value) of the variables used in the functional component. Simply, you cannot interact with the application.</li><li><strong>Class Component</strong> or <strong>Stateful Components: </strong>A class component is a more featured way to define a React component. It also acts as a functional component that receives props and can also control the state of the variables which makes it a <strong>Stateful Component</strong>. Class components have a render function inside them which will return a React element similar to that of the functional component. We can interact with the application if we use a Class component.</li></ol><p>We will understand how the above components work by creating a simple <strong>Hello-World</strong> app.</p><p>Firstly, make a <strong>components</strong> folder inside the <strong>src </strong>folder and then create two files in the <strong>components</strong> folder namely, <strong>Functional.js</strong> and <strong>Class.js.</strong></p><p>The contents of the <strong>Functional.js</strong>:</p><pre>import React from &#39;react&#39;</pre><pre>function Functional() {</pre><pre>return (<br>  &lt;div&gt;<br>    &lt;h1&gt;Hello World&lt;/h1&gt;<br>  &lt;/div&gt;<br> )<br>}</pre><pre>export default Functional</pre><p>The contents of the <strong>Class.js</strong>:</p><pre>import React, { Component } from &#39;react&#39;</pre><pre>class Class extends Component {</pre><pre>render() {<br>  return (<br>    &lt;div&gt;<br>      &lt;h1&gt;Hello World&lt;/h1&gt;<br>    &lt;/div&gt;<br>    )<br>  }<br>}</pre><pre>export default Class</pre><p>To display the components in the application, we must also edit the <strong>App.js </strong>file.</p><p>The contents of the <strong>App.js</strong> file after editing looks like below:</p><pre>import React from &#39;react&#39;;<br>import logo from &#39;./logo.svg&#39;;<br>import &#39;./App.css&#39;;<br>import Functional from &#39;./components/Functional&#39;;<br>import Class from &#39;./components/Class&#39;;</pre><pre>function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;Functional /&gt;<br>      &lt;Class /&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><pre>export default App;</pre><p>After all the above changes the web application looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wK6AAGq7eZB-ch7eBDd7UA.png" /></figure><p>The first <strong>Hello World </strong>from the <strong>functional component</strong> and the second one is from the <strong>class component</strong>. We can reuse the components Functional and Class which are imported in the <strong>App.js </strong>file in any manner we want to. This shows one of the main properties of React which is reusing the components.</p><p><strong>Example</strong>: If we want to have multiple <strong>Hello World’s, </strong>we can simply add the <strong>&lt;Functional /&gt;</strong> or <strong>&lt;Class /&gt; </strong>tags any number of times we want.</p><h4>What are props?</h4><p><strong>Props </strong>is a special keyword in React which is used to pass data from one component to another.</p><p>Let us understand this by taking the same above example which we used in components.</p><p>For this, we need to make changes in the <strong>App.js</strong>, <strong>Class.js</strong>, and <strong>Functional.js </strong>files.</p><p><strong>Functional.js:</strong></p><pre>import React from &#39;react&#39;</pre><pre>function Functional(props) {<br>  return (<br>    &lt;div&gt;<br>      &lt;h1&gt;Hello {props.name}&lt;/h1&gt;<br>    &lt;/div&gt;<br>  )<br>}<br>export default Functional</pre><p><strong>Class.js:</strong></p><pre>import React, { Component } from &#39;react&#39;</pre><pre>class Class extends Component {<br>  render() {<br>    return (<br>      &lt;div&gt;<br>        &lt;h1&gt;Hello {this.props.name}&lt;/h1&gt;<br>      &lt;/div&gt;<br>    )<br>  }<br>}<br>export default Class</pre><p><strong>App.js:</strong></p><pre>import React from &#39;react&#39;;<br>import logo from &#39;./logo.svg&#39;;<br>import &#39;./App.css&#39;;<br>import Functional from &#39;./components/Functional&#39;;<br>import Class from &#39;./components/Class&#39;;</pre><pre>function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;Functional name=&quot;Sam&quot;/&gt;<br>      &lt;Class name=&quot;Mary&quot;/&gt;<br>    &lt;/div&gt;<br>  );<br>}<br>export default App;</pre><p>In the <strong>App.js </strong>file, you can see that the<strong> &lt;Functional /&gt; </strong>tag has a prop called <strong>name</strong> whose value is <strong>Sam </strong>and the<strong> &lt;Class /&gt; </strong>tag also has a prop called name whose value is <strong>Mary.</strong></p><p>In the <strong>Functional.js </strong>and <strong>Class.js</strong> files, you can see that we do not hardcode the name but call it using the <strong>props</strong> keyword of React. Here, the name prop is passed from the App component to Functional and Class components which are in the components folder. Also, note that the Class Component uses <strong>this </strong>keyword to call the props.</p><h4>What is a state and method?</h4><p>React has an inbuilt <strong>state object</strong>. The state object is where you store the values of the properties used in the component. Whenever the state object changes, the component is re-rendered. The change of state is handled by <strong>methods </strong>and a function called <strong>this.setState().</strong></p><p><strong>Let us understand the state objects and methods by creating a React Component:</strong></p><p>We will make a react component that has a <strong>button</strong> in it and render number of times the button is clicked. For this, let us create another file in the <strong>components</strong> folder and name it <strong>Counter.js. </strong>We must also note that the Counter.js component must be a <strong>Class Component</strong> because we are using the <strong>state object</strong> in it</p><p>The contents of<strong> Counter.js:</strong></p><pre>import React, { Component } from &#39;react&#39;</pre><pre>export class Counter extends Component {<br>  constructor(props) {<br>    super(props)</pre><pre>    this.state = {<br>      counter: 0<br>    }<br>  }</pre><pre>  handleClick = (event) =&gt; {<br>    let state_counter = this.state.counter<br>    state_counter = state_counter + 1<br>    this.setState({<br>      counter: state_counter<br>    })<br>  }</pre><pre>  render() {<br>    return (<br>      &lt;div&gt;<br>        &lt;p&gt;{this.state.counter}&lt;/p&gt;<br>        &lt;button onClick={this.handleClick}&gt;Click Me!&lt;/button&gt;<br>      &lt;/div&gt;<br>    )<br>  }<br>}</pre><pre>export default Counter</pre><p>The contents of <strong>App.js:</strong></p><pre>import React from &#39;react&#39;;<br>import logo from &#39;./logo.svg&#39;;<br>import &#39;./App.css&#39;;<br>import Functional from &#39;./components/Functional&#39;;<br>import Class from &#39;./components/Class&#39;;<br>import Counter from &#39;./components/Counter&#39;;</pre><pre>function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;Functional name=&quot;Sam&quot;/&gt;<br>      &lt;Class name=&quot;Mary&quot;/&gt;<br>      &lt;Counter /&gt;<br>    &lt;/div&gt;<br>  );<br>}<br>export default App;</pre><p>You can see it in the <strong>Counter.js </strong>that we declare a <strong>constructor </strong>in which we define the <strong>initial</strong> state of the counter which is 0. The button is passed with an <strong>onClick</strong> event which defines a function <strong>handleClick. </strong>This implies that when the button is clicked, it invokes the function handleClick and performs whatever is given in the function.</p><p>Also, note that the function is associated with <strong>this </strong>keyword and also it is an arrow function. If you do not want to declare the function as an arrow function then you must bind the function call and function definition in the constructor.</p><p><strong>NOTE: IF YOU ARE UNCLEAR WITH THE ABOVE EXPLANATION, JUST REMEMBER TO USE ARROW FUNCTIONS WHENEVER YOU DECLARE A METHOD.</strong></p><p>Inside the method <strong>handleClick, </strong>we can see that we are storing the value of the present state of the counter in the variable state_counter and incrementing it. After incrementing, we use the function <strong>this.setState </strong>to set the new state for the counter. It is also seen that the counter value is rendered by the &lt;p&gt; tag using <strong>this.state.counter.</strong></p><p><strong>The application state after one click</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2QSb20ceuT9IuIoRhIZ9Ug.png" /></figure><p>Here ends the Introduction of React.</p><p>You can find the code for the introduction of react from this <a href="https://github.com/rak-shit/React-Intro-Medium">repo</a>.</p><p>If you have any doubts you can reach me on <a href="https://www.linkedin.com/in/mrakshit/">this</a>.</p><p>Enjoy :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3c0a6c891a8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/introduction-to-react-d3c0a6c891a8">Introduction to React</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>