<?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: Amirah Nasihah</title>
    <description>The latest articles on DEV Community by Amirah Nasihah (@amirahnasihah).</description>
    <link>https://dev.to/amirahnasihah</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%2F843021%2F91af8403-1870-41f0-9406-5434bce2686f.jpg</url>
      <title>DEV Community: Amirah Nasihah</title>
      <link>https://dev.to/amirahnasihah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amirahnasihah"/>
    <language>en</language>
    <item>
      <title>Creating Dynamic Lists in React: A Step-by-Step Guide</title>
      <dc:creator>Amirah Nasihah</dc:creator>
      <pubDate>Sun, 17 May 2026 14:16:25 +0000</pubDate>
      <link>https://dev.to/amirahnasihah/creating-dynamic-lists-in-react-a-step-by-step-guide-5h33</link>
      <guid>https://dev.to/amirahnasihah/creating-dynamic-lists-in-react-a-step-by-step-guide-5h33</guid>
      <description>&lt;h1&gt;
  
  
  Creating Dynamic Lists in React: A Step-by-Step Guide
&lt;/h1&gt;

&lt;p&gt;Lists in React are similar to lists in JavaScript. They are commonly used to display menus or a list of items on a website. &lt;strong&gt;To use lists in React, we need to use a unique &lt;/strong&gt; &lt;code&gt;key&lt;/code&gt; &lt;strong&gt; attribute for each item in the list.&lt;/strong&gt; This is important because it helps React identify which items have been changed, updated or removed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hey there friends! It's been a minute since I last posted on this platform, but I'm back and ready to dive into the world of React once again. Let's get started!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To create a list in React, it is best to &lt;strong&gt;create a new component specifically for the list&lt;/strong&gt;. This component will take in the list data through &lt;code&gt;props&lt;/code&gt;. Inside the component, we can use the &lt;code&gt;map&lt;/code&gt; function to iterate through the list and display each item.&lt;/p&gt;

&lt;p&gt;For the sake of simplicity and learning, this &lt;strong&gt;article will give a simple example of creating a list of posts&lt;/strong&gt;, where the &lt;code&gt;key&lt;/code&gt; is "id" of the post, and the "title" and "body" of the post is displayed. The final output of the tutorial is shown in an embedded code editor. It's recommended to follow this tutorial and practice it several times to better understand the concepts.&lt;/p&gt;

&lt;h1&gt;
  
  
  Code Output
&lt;/h1&gt;

&lt;p&gt;You will get this result once you have finished the tutorial.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/list-and-keys-oyww56?fontsize=14&amp;amp;hidenavigation=1&amp;amp;theme=dark" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fplay-codesandbox.svg" alt="Edit list-and-keys" width="165" height="32"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting Started
&lt;/h1&gt;

&lt;p&gt;To begin, you can create react app using the command line or any code editor (e.g., VSCode). You can also directly use the &lt;a href="https://codesandbox.io/" rel="noopener noreferrer"&gt;CodeSandbox&lt;/a&gt; for an online code editor that is simple to use and allows you to deploy your code.&lt;/p&gt;

&lt;h1&gt;
  
  
  What Components Needed
&lt;/h1&gt;

&lt;p&gt;First, we need to understand what components are required to display a list of blog posts using React. You will need to create at least the following components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A component that retrieves the blog post data from a source, &lt;strong&gt;a Parent component - (App.js).&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A component that renders the list of blog posts, &lt;strong&gt;a Child component - (BlogList.js).&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A reusable component that can be reused in other parts of the application where you want to display a single blog post, &lt;strong&gt;an Inner Child Component - (BlogCard.js).&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can create these components as separate JavaScript files and import them into your main application file, where you can use them to build out the list of blog posts.&lt;/p&gt;

&lt;h1&gt;
  
  
  Data Retrieving Component
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;App.js&lt;/code&gt; - This component would handle retrieving the blog post data from any source. For the sake of simplicity, we will just have a simple array of objects &lt;code&gt;[{key:value}, {key:value}]&lt;/code&gt; and that is the &lt;code&gt;const posts&lt;/code&gt;. Inside them, we have the &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;title&lt;/code&gt;, and &lt;code&gt;body&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will import the BlogList component into the App component and pass the &lt;code&gt;posts&lt;/code&gt; data through props because we want to use the &lt;code&gt;posts&lt;/code&gt; data to be displayed in the BlogList.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;App.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;BlogList&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/BlogList&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;buildspace web3 demo showcase!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Last night's buildspace demo day was a blast, as was go-karting around the map on gather.town dressed as a pumpkin. It's a massive web3 display! Over…&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ReactJS - Introduction&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What is ReactJS? ➡️declarative, efficient, and flexible JavaScript library for building reusable UI components. ➡️an open-source, component-based…&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Build Metaverse with Gather.Town for Free&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Gather.Town Looking for more interactive web-conferencing software like Zoom, but with more fun virtual interactions just like in real life. Then, you…&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BlogList&lt;/span&gt; &lt;span class="na"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Display the Lists Component
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;BlogList.js&lt;/code&gt; - This component would render the list of blog posts using the data retrieved by the &lt;code&gt;App&lt;/code&gt;component and display all the posts on our screen browser.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Inside the BlogList functional component, it will take &lt;code&gt;props&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;BlogCard&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./BlogCard&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BlogList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BlogCard&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;My Posts&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;BlogList&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is a React component named BlogList which maps over an array of blog posts (&lt;code&gt;props.posts&lt;/code&gt;) and returns an array of BlogCard components. Each BlogCard component is rendered with a unique &lt;code&gt;key&lt;/code&gt; prop (&lt;a href="http://post.id" rel="noopener noreferrer"&gt;&lt;code&gt;post.id&lt;/code&gt;&lt;/a&gt;) and passed the whole post data as &lt;code&gt;props&lt;/code&gt; (&lt;code&gt;props={post}&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's go line-by-line through the code:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;import BlogCard from "./BlogCard"&lt;/code&gt;: This line imports a component called &lt;code&gt;BlogCard&lt;/code&gt; from another file called &lt;code&gt;BlogCard.js&lt;/code&gt; in the same directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;const BlogList = (props) =&amp;gt; {...}&lt;/code&gt;: This line creates a new component called &lt;code&gt;BlogList&lt;/code&gt; using a special type of function called an arrow function. This component will receive data from outside the component through its &lt;code&gt;props&lt;/code&gt; argument.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;const content =&lt;/code&gt; &lt;a href="http://props.posts.map" rel="noopener noreferrer"&gt;&lt;code&gt;props.posts.map&lt;/code&gt;&lt;/a&gt;&lt;code&gt;((post) =&amp;gt; {&lt;/code&gt;: This line creates a new constant called &lt;code&gt;content&lt;/code&gt; which will contain an array of components created by mapping over the &lt;code&gt;posts&lt;/code&gt; array received through &lt;code&gt;props&lt;/code&gt;. It maps through an array of posts stored in &lt;code&gt;props&lt;/code&gt; and creates a new array of &lt;code&gt;BlogCard&lt;/code&gt; components for each post where it will return a component for each post. "Mapping through an array" means iterating/looping over the elements of an array and performing a specific operation on each element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;return &amp;lt;BlogCard key={post.id} props={post} /&amp;gt;;&lt;/code&gt;: This line returns a new component of type &lt;code&gt;BlogCard&lt;/code&gt; for each &lt;code&gt;post&lt;/code&gt; in the &lt;code&gt;posts&lt;/code&gt; array. The component will receive data through its &lt;code&gt;props&lt;/code&gt; argument and will have a &lt;code&gt;key&lt;/code&gt; property set to the &lt;code&gt;id&lt;/code&gt; of the current &lt;code&gt;post&lt;/code&gt;. &lt;strong&gt;Remember to use lists in React, we need to have a unique key attribute for each item in the list.&lt;/strong&gt; Otherwise, a warning will appear.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;lt;div&amp;gt;{content}&amp;lt;/div&amp;gt;&lt;/code&gt;: This line returns a &lt;code&gt;div&lt;/code&gt; element that contains all the &lt;code&gt;BlogCard&lt;/code&gt; components created in the previous step. Which embeds the mapped array of &lt;code&gt;BlogCard&lt;/code&gt; components into a div element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;lt;h1&amp;gt;My Posts&amp;lt;/h1&amp;gt;&lt;/code&gt;: This line displays a header element with the text "My Posts".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;lt;div&amp;gt;...&amp;lt;/div&amp;gt;&lt;/code&gt;: This line wraps the header and the &lt;code&gt;content&lt;/code&gt; div in a parent div.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;export default BlogList;&lt;/code&gt;: This line exports the &lt;code&gt;BlogList&lt;/code&gt; component so that it can be used in other files.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The overall &lt;strong&gt;logic of this code&lt;/strong&gt; is to create a component  &lt;code&gt;BlogList&lt;/code&gt;  that displays a list of blog posts using the  &lt;code&gt;BlogCard&lt;/code&gt;  component. The component receives data about the posts through its &lt;code&gt;props&lt;/code&gt; argument, maps over the &lt;code&gt;posts&lt;/code&gt; array to create an array of &lt;code&gt;BlogCard&lt;/code&gt; components, and displays these components in a div along with a header.&lt;/p&gt;

&lt;h1&gt;
  
  
  Reusable Component
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;BlogCard.js&lt;/code&gt; - This component would be a reusable component that takes a single blog post as a prop and displays the details of the blog post in a card format. This component can be reused in other parts of the application where you want to display a single blog post.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This component uses destructuring assignment to extract the "id", "title", and "body" properties from the props object.&lt;/p&gt;

&lt;p&gt;This component in React is called "BlogCard". It takes in a prop called "props", which contains information about a blog post. The component then uses destructuring assignment to extract the "id", "title", and "body" properties from the props object.&lt;/p&gt;

&lt;p&gt;The component then returns a JSX element that &lt;strong&gt;displays the post information in a specific format.&lt;/strong&gt; The id of the post is displayed first, followed by its title, and then its body content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BlogCard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}.&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;BlogCard&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Let's go through line-by-line:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;const BlogCard = ({ props }) =&amp;gt; {...}&lt;/code&gt;: This line creates a new component called &lt;code&gt;BlogCard&lt;/code&gt; using an arrow function. This component will receive data from outside the component through its &lt;code&gt;props&lt;/code&gt; argument.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;const { id, title, body } = props;&lt;/code&gt;: This line uses destructuring to extract the &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;title&lt;/code&gt;, and &lt;code&gt;body&lt;/code&gt; properties from the &lt;code&gt;props&lt;/code&gt; object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;lt;h2&amp;gt;{id}. {title}&amp;lt;/h2&amp;gt;&lt;/code&gt;: This line returns a header element with the &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;title&lt;/code&gt; properties from the &lt;code&gt;props&lt;/code&gt; object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;lt;p&amp;gt;{body}&amp;lt;/p&amp;gt;&lt;/code&gt;: This line returns a paragraph element with the &lt;code&gt;body&lt;/code&gt; property from the &lt;code&gt;props&lt;/code&gt; object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;lt;div&amp;gt;...&amp;lt;/div&amp;gt;&lt;/code&gt;: This line wraps the header and paragraph elements in a parent div.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;export default BlogCard;&lt;/code&gt;: This line exports the &lt;code&gt;BlogCard&lt;/code&gt; component so it can be used in other files.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The overall logic of this code is to create a component &lt;code&gt;BlogCard&lt;/code&gt; that displays a single blog post. The component receives data about the post through its &lt;code&gt;props&lt;/code&gt; argument and displays the post's &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;title&lt;/code&gt;, and &lt;code&gt;body&lt;/code&gt; properties in a header and a paragraph element.&lt;/p&gt;

&lt;h1&gt;
  
  
  Destructuring
&lt;/h1&gt;

&lt;p&gt;Destructuring is a feature in JavaScript that allows you to extract data from arrays or objects and assign them to variables. The main purpose of destructuring is to make it easier and more convenient to access the properties and values of an object or array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// First code snippet&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BlogList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="c1"&gt;// vs&lt;/span&gt;

&lt;span class="c1"&gt;// Second code snippet&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BlogCard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What is the difference between these two? Why are props inside the BlogList not destructured?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The difference between these two code snippets is that in the first code snippet, &lt;code&gt;props&lt;/code&gt; is passed to the &lt;code&gt;BlogList&lt;/code&gt; component as a single argument, and in the second code snippet, &lt;code&gt;props&lt;/code&gt; is destructured from the props object passed to the &lt;code&gt;BlogCard&lt;/code&gt; component.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First code snippet - &lt;code&gt;props&lt;/code&gt; is an object that contains all of the properties that are passed to the &lt;code&gt;BlogList&lt;/code&gt; component. In this case, it is &lt;strong&gt;not necessary to destruct the props object, as the code only needs to access one property,&lt;/strong&gt; &lt;code&gt;posts&lt;/code&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Second code snippet - &lt;code&gt;props&lt;/code&gt; &lt;strong&gt;is destructured from the props object&lt;/strong&gt;, allowing for the properties &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;title&lt;/code&gt;, and &lt;code&gt;body&lt;/code&gt; to be accessed directly. This reduces the need to access properties through the &lt;code&gt;props&lt;/code&gt; object and makes the code more readable and maintainable.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In short, &lt;strong&gt;the difference is in the way that the properties are accessed within the component&lt;/strong&gt;. The &lt;strong&gt;first code snippet accesses the properties through the &lt;/strong&gt; &lt;code&gt;props&lt;/code&gt; &lt;strong&gt; object, while the second code snippet accesses the properties directly by destructuring the &lt;/strong&gt; &lt;code&gt;props&lt;/code&gt; &lt;strong&gt; object.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Full Code
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;BlogList&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/BlogList&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;buildspace web3 demo showcase!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Last night's buildspace demo day was a blast, as was go-karting around the map on gather.town dressed as a pumpkin. It's a massive web3 display! Over…&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ReactJS - Introduction&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;What is ReactJS? ➡️declarative, efficient, and flexible JavaScript library for building reusable UI components. ➡️an open-source, component-based…&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Build Metaverse with Gather.Town for Free&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Gather.Town Looking for more interactive web-conferencing software like Zoom, but with more fun virtual interactions just like in real life. Then, you…&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;BlogList&lt;/span&gt; &lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;BlogList.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;BlogCard&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./BlogCard&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BlogList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;BlogCard&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;My&lt;/span&gt; &lt;span class="nx"&gt;Posts&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;BlogList&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;BlogCard.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;BlogCard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}.&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;BlogCard&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;👉 Please share my posts with the community at &lt;a href="http://daily.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;daily.dev&lt;/strong&gt;&lt;/a&gt; / social media by adding the article's URL to the feed. By adding my article's URL to the feed, I can share my insights and knowledge with other tech enthusiasts and contribute to the passionate community.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cheers✨&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thank you for your continued support and for sticking around even though I've been away for a bit. I've been busy with some exciting projects, but I'm thrilled to be back and writing again!&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Dev Retro 2022: My Story of Switching Careers as a Beginner Developer</title>
      <dc:creator>Amirah Nasihah</dc:creator>
      <pubDate>Sun, 17 May 2026 14:16:23 +0000</pubDate>
      <link>https://dev.to/amirahnasihah/dev-retro-2022-my-story-of-switching-careers-as-a-beginner-developer-ldm</link>
      <guid>https://dev.to/amirahnasihah/dev-retro-2022-my-story-of-switching-careers-as-a-beginner-developer-ldm</guid>
      <description>&lt;h1&gt;
  
  
  Dev Retro 2022: My Story of Switching Careers as a Beginner Developer
&lt;/h1&gt;

&lt;p&gt;I made the &lt;strong&gt;bold move&lt;/strong&gt; to switch career paths from &lt;strong&gt;accounting to coding&lt;/strong&gt; during the pandemic. Now, a year later, I'm working at a tech company and have made some amazing friends. While I've faced a lot of pressure and seen my friends go on to achieve professional certifications, I've still been struggling with my decision to change careers.&lt;/p&gt;

&lt;p&gt;As I look back on my journey in the tech world, I can see all the challenges and accomplishments I've experienced. And I want to share those insights and lessons with all of you through this &lt;code&gt;#DevRetro2022&lt;/code&gt;. Get ready for the long wild ride!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdy04mhkef2whortwpjbl.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdy04mhkef2whortwpjbl.gif" width="500" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Here we go!🏌️
&lt;/h1&gt;

&lt;p&gt;So, I started by learning Python with the goal of improving my accounting skills with data analysis. Along the way, I discovered HTML and CSS and I was immediately hooked on web design.&lt;/p&gt;

&lt;p&gt;I spent about two months learning these languages through resources like &lt;a href="https://www.freecodecamp.org/" rel="noopener noreferrer"&gt;freeCodeCamp&lt;/a&gt; and &lt;a href="https://www.sololearn.com/" rel="noopener noreferrer"&gt;SoloLearn&lt;/a&gt;. I learned about Git and GitHub, especially the part about beautifying my &lt;a href="https://github.com/amirahnasihah" rel="noopener noreferrer"&gt;README&lt;/a&gt;. Eventually, I realized that I needed to learn JavaScript for more interactive and dynamic web pages, and I started learning it as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ztjqsu2ucy4fa7a6xpf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ztjqsu2ucy4fa7a6xpf.png" width="800" height="623"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  This Year I Achieved...
&lt;/h1&gt;

&lt;p&gt;Now onto the terrific part, some of the great things I've achieved this year🏆&lt;/p&gt;

&lt;h2&gt;
  
  
  🔸Accepted Into Software Engineering Course
&lt;/h2&gt;

&lt;p&gt;I was accepted into a free &lt;a href="https://www.talentlabs.org/" rel="noopener noreferrer"&gt;Foundation of Software Engineering course&lt;/a&gt;, where I met a group of amazing people who were all trying to switch careers into tech. Most of us had no technical background at all – there was a cinema assistant manager, a nurse, a psychiatrist, and more.&lt;/p&gt;

&lt;p&gt;I also learned about useful tools like AWS, Bootstrap, and Git. I even shared my excitement about the course on Twitter and had a lot of people DM me asking for tips on applying and my experience with the program.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkqtjj78qr3i8nk3bc9p3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkqtjj78qr3i8nk3bc9p3.png" width="800" height="565"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After completing the Foundation course, I was offered the opportunity to move on to an advanced course in front-end development. This course has been challenging, but I've learned a lot about complex concepts like programming, networking, and domains. I'm currently building three challenging projects using React. It's been tough balancing this coursework with a full-time job, but it's all worth it.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔸Featured On Hashnode 🎉
&lt;/h2&gt;

&lt;p&gt;My &lt;a href="https://amirahnasihah.hashnode.dev/prompt-101-generate-cool-stable-diffusion-images-with-this-guide" rel="noopener noreferrer"&gt;article&lt;/a&gt; got featured on Hashnode for the very first time! I had been writing with the Hashnode community for about a year, and it was such a cool feeling to see one of my articles get picked for the spotlight. It was about how to generate cool, stable diffusion images using a guide I'd put together. This was a huge honour, and it was exciting to see my work being recognized by the community.&lt;/p&gt;

&lt;p&gt;Things I learned -- SEO, plotting my articles, how to make my title and article attractive, how to use Canva for an aesthetic look on the cover post, how to increase the article's engagement and more!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd7a5o6lmm2ms01pwleqc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd7a5o6lmm2ms01pwleqc.png" width="800" height="530"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🔸Job At Tech Company
&lt;/h2&gt;

&lt;p&gt;I'm excited that I landed a job offer at a &lt;a href="https://www.ever-technologies.com/" rel="noopener noreferrer"&gt;start-up tech company&lt;/a&gt;! The company's focus is on &lt;a href="https://www.linkedin.com/company/ever-ai-technologies/mycompany/" rel="noopener noreferrer"&gt;using AI and Blockchain technology&lt;/a&gt; to educate non-technical people and help them learn. I'm currently working as a system engineering intern, which I was initially a bit hesitant about since it's different from what I'm studying or interested in.&lt;/p&gt;

&lt;p&gt;However, after talking to a mentor and considering the opportunity as a whole, I realized that this job could provide valuable experience and exposure to the tech industry, as well as new skills and knowledge that could help me achieve my long-term career goals. So, I can't wait to see where this opportunity takes me!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmhrv7x9ok8xtri621497.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmhrv7x9ok8xtri621497.png" width="800" height="89"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🔸Networking + Cool Events + Epic Stuff
&lt;/h2&gt;

&lt;p&gt;This year, I've attended a number of virtual events that have helped me make valuable connections and grow as a developer. One of the most inspiring events for me was the &lt;a href="https://buildspace.so/" rel="noopener noreferrer"&gt;buildspace N&amp;amp;W Demo Day&lt;/a&gt;, where I had the chance to meet other genius developers and learn about Web3. It was an amazing experience that helped me think about my career in a new way. I'm grateful for the opportunities I've had to connect with others in the tech industry.&lt;/p&gt;

&lt;p&gt;%[&lt;a href="https://www.youtube.com/watch?v=QtCNRO5kh3U" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=QtCNRO5kh3U&lt;/a&gt;] &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5aauistvw2zpixc0c15r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5aauistvw2zpixc0c15r.png" width="800" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;To Wrap Up!&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;My journey hasn't just been about achievements – there have been plenty of challenges along the way, too. I also struggled with imposter syndrome and the decision to switch careers in the first place. It can be tough to make such a big change, especially when you're not sure if you're cut out for it. But through it all, I learned to embrace the challenges and keep pushing forward.💪&lt;/p&gt;

&lt;p&gt;So, if you're considering making a switch to tech or just starting out as a beginner developer, don't be afraid to take the leap. It can be intimidating, but it's worth it in the end. The tech industry is super diverse - there's something for everyone! Whether you're interested in building software, analyzing data, or designing user experiences, there's a career path for you.&lt;/p&gt;

&lt;p&gt;And if you're struggling, remember that you're not alone and that it's okay to ask for help. I hope my journey has inspired you and that you'll join me on this fantastic ride.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thank-you note!❤️&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Just wanted to drop a quick note to say thank you for all your help, support and trust. I couldn't have done it without each one of you! Thanks to my family, friends, Tech community, TalentLabs, Ever AI, Hashnode and many more!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;If you enjoyed this, please show some love by liking💖, commenting🤟, and sharing🙌. Your support means a lot and I love reading your thoughts. Thanks for reading!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Happy coding!✨&lt;/code&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>career</category>
      <category>codenewbie</category>
      <category>devjournal</category>
    </item>
    <item>
      <title>Prompt 101: Generate Cool Stable Diffusion Images with this Guide</title>
      <dc:creator>Amirah Nasihah</dc:creator>
      <pubDate>Sun, 17 May 2026 14:16:22 +0000</pubDate>
      <link>https://dev.to/amirahnasihah/prompt-101-generate-cool-stable-diffusion-images-with-this-guide-4len</link>
      <guid>https://dev.to/amirahnasihah/prompt-101-generate-cool-stable-diffusion-images-with-this-guide-4len</guid>
      <description>&lt;h1&gt;
  
  
  Prompt 101: Generate Cool Stable Diffusion Images with this Guide
&lt;/h1&gt;

&lt;p&gt;Hey there, if you have tried using Stable Diffusion and are unsure of how others are able to generate such impressive images, then this is the perfect place for you to learn the secret behind creating impressive images using AI.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpfbecs3bx1xuc751p9wu.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpfbecs3bx1xuc751p9wu.gif" width="160" height="160"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may have generated amazing images using AI and you felt like it was a result of chance. However, there is a technique behind this seemingly random process. It's called prompt engineering, and it's not just magic, it's science!&lt;/p&gt;

&lt;h1&gt;
  
  
  What is Prompt
&lt;/h1&gt;

&lt;p&gt;Prompt engineering is a technique used to guide AI to generate high-quality and relevant text, and by experimenting with different prompt structures and formats, you can improve the output of your own prompts.&lt;/p&gt;

&lt;p&gt;Sure, creating effective prompts for AI may be a challenge, especially for those new to prompt engineering. They may struggle to get the desired results from their AI, causing frustration and disappointment. But the good news is that anyone can learn how to talk to AI and generate impressive images.&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting Started
&lt;/h1&gt;

&lt;p&gt;I'll use &lt;a href="https://lexica.art/" rel="noopener noreferrer"&gt;Lexica.art&lt;/a&gt; to make things easy. It's a Stable Diffusion search engine that also allows you to quickly and easily generate impressive images using prompts directly on the site.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhymvv0wtelj1nn925hjm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhymvv0wtelj1nn925hjm.png" width="800" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To create a super duper image using &lt;a href="http://Lexica.art" rel="noopener noreferrer"&gt;Lexica.art&lt;/a&gt;, just follow these simple steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open &lt;a href="http://Lexica.art" rel="noopener noreferrer"&gt;Lexica.art&lt;/a&gt; in your web browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On the homepage, click on the 'Generate' button.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You will be presented with a blank canvas where you can input your prompt.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To create your prompt, you can type in natural language (e.g. "a red apple on a white table, an astronaut riding a horse").&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the 'Generate' page, click on the icon next to the prompter and set the guidance scale to 10 and the image size to 864x512.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the 'Generate' button to generate the SD image.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The generated image will be displayed on the page, and when you're happy, you can "Download" it or share it using the options provided.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can also adjust the other settings on the page to customize the generated image, but we'll leave that for another time.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these, you can quickly and easily generate an SD image using &lt;a href="http://Lexica.art" rel="noopener noreferrer"&gt;Lexica.art&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb16gmo642hbd76yweuh5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb16gmo642hbd76yweuh5.jpg" width="800" height="474"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To generate the best images using AI, it is important to break down the prompt into clear and concise instructions.&lt;/p&gt;

&lt;p&gt;Here's the prompt for the above image:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;illustration of an astronaut lounging in a tropical resort in space by dan seagrave, spiral clouds falling, highly detailed, surrealism, triadic color scheme, smooth, sharp focus, matte, elegant, dark, gloomy, octane render, 8k, 4k, washed colors, sharp, dramatic lighting, beautiful, post processing, ambient lighting, epic composition&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s look at the POV of the prompt formula.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s"&gt;illustration (this is my style choice) of&lt;/span&gt;

&lt;span class="s"&gt;an astronaut lounging in a tropical resort in space (the thing I want in the image) + by dan seagrave (artist style),&lt;/span&gt;

&lt;span class="s"&gt;spiral clouds falling (more stuff I want in the image),&lt;/span&gt;

&lt;span class="s"&gt;and the rest are all details on how I would like the image to look.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great, now you have everything you need to create some amazing images!&lt;/p&gt;

&lt;h1&gt;
  
  
  TL;DR: Prompt Formula
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;TL;DR&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Prompt Formula&lt;/span&gt;

&lt;span class="s"&gt;Art Medium + The actual things you want in the image + Art Styles (could be an artist or an actual style like Anime, ) + Modifications (extra details on how the image should look) ⇒ Good Image.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To generate images that match your imagination, it may take several attempts to get a result you like. Repeat the process a few times using the same prompt before tweaking the prompt or adjusting the settings to make it more specific or detailed.&lt;/p&gt;

&lt;p&gt;After trying a few times, you can start to adjust the prompt to create the exact image you want. Practice is key to developing a strong understanding of how AI works and how to use it as an artistic tool.&lt;/p&gt;

&lt;p&gt;Generate multiple images using the same prompt to see how the results vary. You may find that the same prompt produces different results each time, so it's worth trying several times to see which image you like best.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Ready to take your image-making skills to the next level? Try experimenting with &lt;a href="https://amirahnasihah.hashnode.dev/how-to-start-generate-ai-art-for-noob" rel="noopener noreferrer"&gt;different prompts and AI tools, like Midjourney&lt;/a&gt;, to create some truly stunning images. The possibilities are endless, so get creative and have fun! 🎉&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;P.S.: I'm using a Hashnode &lt;strong&gt;WYSIWYG&lt;/strong&gt; editor -- called &lt;a href="https://hashnode.com/neptune" rel="noopener noreferrer"&gt;&lt;strong&gt;Neptune&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  &lt;strong&gt;Share This Tutorial&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;👉 Please share my posts with the community at &lt;a href="http://daily.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;daily.dev&lt;/strong&gt;&lt;/a&gt; / social media by adding the article's URL to the feed.&lt;/p&gt;

&lt;p&gt;By adding my article, I can share my insights and knowledge with other tech enthusiasts and contribute to the passionate community.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cheers✨&lt;/code&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How To Start Generate AI Art For Noob</title>
      <dc:creator>Amirah Nasihah</dc:creator>
      <pubDate>Sun, 17 May 2026 14:16:20 +0000</pubDate>
      <link>https://dev.to/amirahnasihah/how-to-start-generate-ai-art-for-noob-13cc</link>
      <guid>https://dev.to/amirahnasihah/how-to-start-generate-ai-art-for-noob-13cc</guid>
      <description>&lt;h1&gt;
  
  
  How To Start Generate AI Art For Noob
&lt;/h1&gt;

&lt;p&gt;hey👀, if you're interested in generating AI art but don't know where to start -- this blog post is for you!&lt;/p&gt;

&lt;p&gt;I'll cover the top AI-based art website that you need in order to get started, including some helpful resources and tips. 🙌&lt;/p&gt;

&lt;h3&gt;
  
  
  1️⃣ Lexica search engine
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://lexica.art/" rel="noopener noreferrer"&gt;https://lexica.art/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To become proficient at creating AI art, you must first learn how prompts work. What you ask the AI art generator to produce is prompt. Lexica is a website similar to Google that displays AI art as well as the prompts used to produce it. It's a great way to find prompts for your own ideas.&lt;/p&gt;

&lt;h3&gt;
  
  
  2️⃣ Artbreeder
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.artbreeder.com/" rel="noopener noreferrer"&gt;https://www.artbreeder.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are many websites that offer AI art for beginners, but one of the best is Artbreeder. Artbreeder offers a wide range of options for generating AI art, and the results can be very realistic. The website is easy to use and offers a wide range of tutorials to help you get started.&lt;/p&gt;

&lt;h3&gt;
  
  
  3️⃣ NightCafe
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://creator.nightcafe.studio/" rel="noopener noreferrer"&gt;https://creator.nightcafe.studio/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you're ready to create higher-quality images for usage or want greater control over the process, try NightCafe. Every day, they provide 5 free credits that you may use. If you run out of credits, you can purchase more or just wait until the following day to claim more.&lt;/p&gt;

&lt;h3&gt;
  
  
  4️⃣ OpenAI's DALL-E 2
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://labs.openai.com/" rel="noopener noreferrer"&gt;https://labs.openai.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DALL-E is an artificial intelligence program that dazzles with its capacity to transform the text into lifelike pictures. You get 50 free credits for the first month and 15 free credits after that.&lt;/p&gt;

&lt;h3&gt;
  
  
  5️⃣ Midjourney
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://midjourney.com/" rel="noopener noreferrer"&gt;https://midjourney.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;They use the Discord server to generate an image using AI. Midjourney allows you to create your first 25 photos for free. But you won't be able to stop at 25! Offering a basic membership with a monthly subscription and a higher-level standard membership that allows you to generate unlimited images.&lt;/p&gt;

&lt;h3&gt;
  
  
  6️⃣ Stable Diffusion web demo
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://huggingface.co/spaces/stabilityai/stable-diffusion" rel="noopener noreferrer"&gt;https://huggingface.co/spaces/stabilityai/stable-diffusion&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try out the Stable Diffusion online demo above. It functions similarly to Stable Diffusion, but with less adjustability and lower-quality pictures. Since you can make an infinite number of photos, it's fun to experiment with different prompts.&lt;/p&gt;

&lt;h3&gt;
  
  
  7️⃣ Installing and using Stable Diffusion
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=pA-njBq1qmM" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=pA-njBq1qmM&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Installing and running Stable Diffusion on your own machine provides you with the most control and flexibility when creating AI art. You can generate an endless number of images and use custom models and features like Initial Image (img2img). It provides a lot of features and options that can be very helpful for users.&lt;/p&gt;

&lt;h1&gt;
  
  
  🔥 AI generated Images
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftj1li7mt4bw7le0onm22.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftj1li7mt4bw7le0onm22.png" alt="amrhnshh_illustration_of_study_room_of_computer_desktop_heading_107c3646-5dbb-48cd-94c7-0faa96919cda.png" width="800" height="533"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8wwc4s2cn2mt8mbiaamu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8wwc4s2cn2mt8mbiaamu.png" alt="amrhnshh_a_zombie_robot_eating_spagetti_8051e19c-e54a-4589-ad00-e873a8b4d47a (1).png" width="800" height="533"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fltpi28923ntxxv74gbyf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fltpi28923ntxxv74gbyf.png" width="800" height="533"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdb63jlh4h9nx925y38a5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdb63jlh4h9nx925y38a5.png" width="800" height="1200"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjeoi0oa8ac1s37xgi9yx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjeoi0oa8ac1s37xgi9yx.png" width="800" height="1200"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwoqymyyksro5k1plu05a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwoqymyyksro5k1plu05a.png" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;👀 Show me what you got!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/intent/tweet?text=Playing%20around%20w%2F%20ML%20%2B%20Stable%20Diffusion%21%20%E2%9C%A8%0A%0AHere%20are%20a%20few%20images%20I%20generated%20today%20via%20prompts%20I%20created.%0A%0AKudos%20to%20%40amrhnshh%20for%20the%20post!%21%0A%0A%3Ccopy%20paste%20your%204%20fav%20generated%20images%20here%3E%0A%0A%3Cif%20you%20feel%20like%20it%2C%20add%20another%20tweet%20with%20a%20link%20to%20the%20guide%21%3E" rel="noopener noreferrer"&gt;You've got something cool, let's see it! Click here to show the world and I'd love to check it out.&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;P.S.: I'll write another post regarding &lt;a href="https://amirahnasihah.hashnode.dev/prompt-101-generate-cool-stable-diffusion-images-with-this-guide" rel="noopener noreferrer"&gt;how to Master Diffusion Image Generation with this Comprehensive Guide&lt;/a&gt;. Let's connect and follow up with me!&lt;/p&gt;

&lt;p&gt;👉 Please share my posts with the community at &lt;a href="http://daily.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;daily.dev&lt;/strong&gt;&lt;/a&gt; / social media by adding the article's URL to the feed.&lt;/p&gt;

&lt;p&gt;By adding my article, I can share my insights and knowledge with other tech enthusiasts and contribute to the passionate community.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cheers✨&lt;/code&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Changing Button's Text using React</title>
      <dc:creator>Amirah Nasihah</dc:creator>
      <pubDate>Sun, 17 May 2026 14:16:18 +0000</pubDate>
      <link>https://dev.to/amirahnasihah/changing-buttons-text-using-react-46nc</link>
      <guid>https://dev.to/amirahnasihah/changing-buttons-text-using-react-46nc</guid>
      <description>&lt;h1&gt;
  
  
  Changing Button's Text using React
&lt;/h1&gt;

&lt;p&gt;In this tutorial, I am going to give you a step-by-step guide how to change the button text from &lt;code&gt;On&lt;/code&gt; to &lt;code&gt;Off&lt;/code&gt; while understanding the concepts of conditional rendering in React application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conditional Rendering
&lt;/h1&gt;

&lt;p&gt;In React, you often need to display different things depending on different conditions. You can conditionally render JSX using JavaScript syntax like &lt;code&gt;if&lt;/code&gt; statements, &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;, and &lt;code&gt;? :&lt;/code&gt; operators.&lt;/p&gt;

&lt;h1&gt;
  
  
  To Start
&lt;/h1&gt;

&lt;p&gt;To begin, you can create react app using the command line or any code editor (e.g., VSCode). You can also directly use the &lt;a href="https://codesandbox.io/" rel="noopener noreferrer"&gt;CodeSandbox&lt;/a&gt; for an online code editor that is simple to use and allows to deploy your code.&lt;/p&gt;

&lt;h1&gt;
  
  
  Code Output
&lt;/h1&gt;

&lt;p&gt;This is the result that you will achieve once you have finished the tutorial. When you clicked the button, the text &lt;code&gt;On&lt;/code&gt; will change to &lt;code&gt;Off&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The best practice is to read through this tutorial and follow along several times to have a better knowledge of what you're doing with this concept in React.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fplay-codesandbox.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fplay-codesandbox.svg" alt="Edit button-ON-to-OFF" width="165" height="32"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Update the Button Text (On to Off)
&lt;/h1&gt;

&lt;p&gt;So, we know that in React, we can create multiple components and we can render the components based on the state of the application.&lt;/p&gt;

&lt;p&gt;So, what I'm going to do is to create a new folder named 'components'. And inside the folder, let us have a component file named &lt;code&gt;ButtonControl.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, we go to that component file and I will have the function &lt;code&gt;ButtonControl&lt;/code&gt;. Then, I'm going to export default the ButtonControl component. Like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ButtonControl.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ButtonControl&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ButtonControl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, inside the ButtonControl component, I'm going to create two arrow functions.&lt;/p&gt;

&lt;p&gt;The first function will be the &lt;code&gt;handleOnClick()&lt;/code&gt; function and the second function which will be the &lt;code&gt;handleOffClick()&lt;/code&gt; function. We will have it like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ButtonControl.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ButtonControl&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOnClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOffClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ButtonControl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;code&gt;handleOnClick&lt;/code&gt; And &lt;code&gt;handleOffClick&lt;/code&gt; Explained
&lt;/h1&gt;

&lt;p&gt;First, let's create a state. So, we have a state and need to import the &lt;code&gt;useState&lt;/code&gt; because we want to use the state.&lt;/p&gt;

&lt;p&gt;So, I'm going to have the &lt;code&gt;useState&lt;/code&gt;, and this &lt;code&gt;useState&lt;/code&gt; will be coming from &lt;code&gt;'react'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Alright, and now what we are going to do is, in this React state array &lt;code&gt;[]&lt;/code&gt;, I'm going create a state variable and the name of the state variable will be &lt;code&gt;isClickedOn&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, if we want to update this state variable, we will have a function which will be &lt;code&gt;setIsClickedOn&lt;/code&gt;. The initial value of this state will be &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ButtonControl.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ButtonControl&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isClickedOn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsClickedOn&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOnClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOffClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ButtonControl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, we are done.&lt;/p&gt;

&lt;p&gt;And this is what going to happen when you have the &lt;code&gt;handleOnClick&lt;/code&gt; and &lt;code&gt;handleOffClick&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What I want is for whenever a user clicked the &lt;code&gt;On&lt;/code&gt; button, it will show the &lt;code&gt;Off&lt;/code&gt; button. If a user clicked &lt;code&gt;Off&lt;/code&gt;, it will show the &lt;code&gt;On&lt;/code&gt; button.&lt;/p&gt;

&lt;p&gt;And what the &lt;code&gt;ButtonControl&lt;/code&gt; component is going to return here is a JSX.&lt;/p&gt;

&lt;p&gt;I'm going to create a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; tag, and inside the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;. I'm going to have a &lt;code&gt;button&lt;/code&gt; function so it will display a button based on the condition of &lt;code&gt;isClickedOn&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;if it's clicked On, then it will show the Off button.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if it's clicked Off, then it will show the On button.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ButtonControl.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, that is where we need to create a condition and now, we need to define what this button is. What I will do is that I am going to have a &lt;code&gt;let button&lt;/code&gt;. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this &lt;code&gt;let button&lt;/code&gt;, what we will do is we will check that if we have &lt;code&gt;isClickedOn&lt;/code&gt;, if it is &lt;code&gt;true&lt;/code&gt;, then we have a button that is going to show &lt;code&gt;Off&lt;/code&gt;. So. let's have a component of that &lt;code&gt;&amp;lt;OffButton /&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ButtonControl.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isClickedOn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;OffButton&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, we will have it like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ButtonControl.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ButtonControl&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isClickedOn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsClickedOn&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOnClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOffClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isClickedOn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;OffButton&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ButtonControl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  OnButton and OffButton Components
&lt;/h1&gt;

&lt;p&gt;Before continuing doing this, let's create two more components which will be the &lt;code&gt;OffButton&lt;/code&gt; component and the &lt;code&gt;OnButton&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;What we can do is instead of creating a component in a separate file, we are going to create in the same file here at &lt;code&gt;ButtonControl.js&lt;/code&gt; a &lt;code&gt;OnButton&lt;/code&gt; component as well as the &lt;code&gt;OffButton&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;So, I will have the &lt;code&gt;OnButton()&lt;/code&gt; function and this &lt;code&gt;OnButton()&lt;/code&gt; is going to take some &lt;code&gt;props&lt;/code&gt;. And when it has a prop it is going to return a JSX.&lt;/p&gt;

&lt;p&gt;So, what it is going to return is that it will return a &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; tag and on that button, we are going to have an &lt;code&gt;onClick&lt;/code&gt; event.&lt;/p&gt;

&lt;p&gt;Alright, here we are going to have the &lt;strong&gt;On&lt;/strong&gt; text. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;OnButton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;On&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now similarly, we will also have that &lt;code&gt;OffButton()&lt;/code&gt; function, so we can just copy the &lt;code&gt;OnButton()&lt;/code&gt; function and it will have that function called &lt;code&gt;OffButton()&lt;/code&gt; with the &lt;strong&gt;Off&lt;/strong&gt; text. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;OffButton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Off&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, there is a prop in both components of the &lt;code&gt;OnButton(props)&lt;/code&gt; and &lt;code&gt;OffButton(props)&lt;/code&gt; we are passing.&lt;/p&gt;

&lt;p&gt;So, we have to pass some prop in here &lt;code&gt;&amp;lt;OffButton /&amp;gt;&lt;/code&gt; in the ButtonControl component.&lt;/p&gt;

&lt;p&gt;I am going to pass a prop as &lt;code&gt;onClick&lt;/code&gt; property and our &lt;code&gt;onClick&lt;/code&gt; will have a handler which will be a &lt;code&gt;handleOffClick&lt;/code&gt; arrow function. It will be like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ButtonControl.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isClickedOn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;OffButton&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleOffClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if it is not the case then else, what I have to show is I going to have to show a &lt;code&gt;OnButton&lt;/code&gt;. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isClickedOn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;OffButton&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleOffClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;OnButton&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleOnClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will save it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Experimenting with The State As &lt;code&gt;true&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;And then, we need to do something that whenever we click on the handle events, where whenever we click on the Off, we have to change the state.&lt;/p&gt;

&lt;p&gt;So this state of &lt;code&gt;useState(false)&lt;/code&gt; will get changed to &lt;strong&gt;'the false one'&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And I'm going to change the state &lt;code&gt;setIsClickedOn()&lt;/code&gt; as &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Alright, now this &lt;code&gt;setIsClickedOn(true)&lt;/code&gt; will become false whenever you click the &lt;code&gt;handleOffClick&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;So, let's have this on &lt;code&gt;handleOffClick&lt;/code&gt; too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isClickedOn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsClickedOn&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOnClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nf"&gt;setIsClickedOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOffClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nf"&gt;setIsClickedOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After we are done doing this, let's call the component.&lt;/p&gt;

&lt;p&gt;We go to the &lt;code&gt;App.js&lt;/code&gt; to call this &lt;code&gt;ButtonControl&lt;/code&gt; component. Let's import the ButtonControl from the components folder to use the component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ButtonControl&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/ButtonControl&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ButtonControl&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, we know that in React, we create multiple components and we can render the components based on the state of the application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Button On Not Changing to Off
&lt;/h1&gt;

&lt;p&gt;Let's import the ButtonControl component and have the &lt;code&gt;&amp;lt;ButtonControl /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then, we will save it.&lt;/p&gt;

&lt;p&gt;And now, we see that we have a parsing error and that error is in the &lt;code&gt;ButtonControl&lt;/code&gt; states that parsing JSX attributes must only be assigned a non-empty expression.&lt;/p&gt;

&lt;p&gt;So, let's go back to the &lt;code&gt;ButtonControl.js&lt;/code&gt; and let see the line number where the error occurred.&lt;/p&gt;

&lt;p&gt;Here, we have to specify what prop we have passed in the prop is &lt;code&gt;props.onClick&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's pass the &lt;code&gt;onClick&lt;/code&gt; here in &lt;code&gt;OnButton()&lt;/code&gt; function and similarly with &lt;code&gt;OffButton()&lt;/code&gt; function. In the end it will look like the below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;ButtonControl.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;OnButton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;ON&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;OffButton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;OFF&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, and save it.&lt;/p&gt;

&lt;p&gt;Now, if we refresh we will see on the screen we have that &lt;code&gt;On&lt;/code&gt; button.&lt;/p&gt;

&lt;p&gt;So, whenever I click on this On button on the screen, it will change to Off.&lt;/p&gt;

&lt;p&gt;However, when I click the button again, it is &lt;strong&gt;NOT&lt;/strong&gt; changing to On again.&lt;/p&gt;

&lt;p&gt;That means we have made a mistake and this mistake you will see here on &lt;code&gt;handleOffClick()&lt;/code&gt; function that we have to change the value back to &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOffClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nf"&gt;setIsClickedOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if I click on the button, then you will see that we have a toggle like ON and OFF button.&lt;/p&gt;

&lt;p&gt;So, in our case the state was &lt;code&gt;isClickedOn&lt;/code&gt; and based on the &lt;code&gt;isClickedOn&lt;/code&gt;, we are showing different components like the &lt;code&gt;OffButton&lt;/code&gt; component or the &lt;code&gt;OnButton&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;This way you can actually do the conditional rendering in React where you can show the components based on the state of the application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Full Code
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ButtonControl&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/ButtonControl&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ButtonControl&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;ButtonControl.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ButtonControl&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isClickedOn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsClickedOn&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOnClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setIsClickedOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOffClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setIsClickedOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isClickedOn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;OffButton&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleOffClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;OnButton&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleOnClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ButtonControl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// BUTTON TEXT&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;OnButton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;ON&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;OffButton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;OFF&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;👉 Please share my posts with the community at &lt;a href="http://daily.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;daily.dev&lt;/strong&gt;&lt;/a&gt; / social media by adding the article's URL to the feed. By adding my article's URL to the feed, I can share my insights and knowledge with other tech enthusiasts and contribute to the passionate community.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cheers✨&lt;/code&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Event Handling for React Beginners - Tutorial Example Code</title>
      <dc:creator>Amirah Nasihah</dc:creator>
      <pubDate>Sun, 17 May 2026 14:16:16 +0000</pubDate>
      <link>https://dev.to/amirahnasihah/event-handling-for-react-beginners-tutorial-example-code-2802</link>
      <guid>https://dev.to/amirahnasihah/event-handling-for-react-beginners-tutorial-example-code-2802</guid>
      <description>&lt;h1&gt;
  
  
  Event Handling for React Beginners - Tutorial Example Code
&lt;/h1&gt;

&lt;p&gt;This tutorial is going to give you a step-by-step guide on understanding one of the main concepts of React which is handling events in React application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Handling Events in React
&lt;/h1&gt;

&lt;p&gt;Handling events with React elements is very similar to handling events on DOM elements. There are some syntax differences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;React events are named using camelCase, rather than lowercase.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;With JSX you pass a function as the event handler, rather than a string.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What I want is for when we click the button toggle, it will update or change it to the &lt;code&gt;Clock&lt;/code&gt; component. That's why we need the &lt;code&gt;useState&lt;/code&gt; hook to make that changes. And to change the button, we use the initial value of the boolean value of &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  First Step
&lt;/h1&gt;

&lt;p&gt;Here's a heads-up, this is the continuous tutorial of the state and lifecycle method where what I want to do is to manipulate the button from showing the Clock component that is updated every second to the No Clock component that is shown on the screen.&lt;/p&gt;

&lt;p&gt;To begin, you can create a react app using the command line or any code editor (e.g., VSCode). You can also try using &lt;a href="https://codesandbox.io/" rel="noopener noreferrer"&gt;CodeSandbox&lt;/a&gt; as an online code editor that is simple to use and allows you to deploy your code.&lt;/p&gt;

&lt;p&gt;The best practice is to read through this tutorial and follow along several times to have a better understanding of handling events in React.&lt;/p&gt;

&lt;h1&gt;
  
  
  Code Output
&lt;/h1&gt;

&lt;p&gt;This is the result that you will achieve once you have finished the tutorial. My suggestion is to read it through, then go through it again one step at a time using the code editor and repeat until you can do it by yourself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fplay-codesandbox.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fplay-codesandbox.svg" alt="Edit event-handling-toggle-clock" width="165" height="32"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Create a Button
&lt;/h1&gt;

&lt;p&gt;Let's create an &lt;code&gt;useState&lt;/code&gt; in the &lt;code&gt;App.js&lt;/code&gt; file. The initial value of state is set to &lt;code&gt;true&lt;/code&gt;. We will look into it more below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we create a &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; tag inside the returned fragment. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in this button, let's have a &lt;code&gt;onClick&lt;/code&gt;. And on the &lt;code&gt;onClick&lt;/code&gt;, I am going to give a function name, which is a &lt;code&gt;toggle&lt;/code&gt; function. And the placeholder will be &lt;code&gt;"Toggle Clock Component"&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;toggle&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Toggle Clock Component&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we see that we have an &lt;strong&gt;error&lt;/strong&gt; because the toggle function is not defined.&lt;/p&gt;

&lt;p&gt;So, let's have a function above the return, called &lt;code&gt;toggle&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Let's create a function &lt;code&gt;toggle()&lt;/code&gt; inside the &lt;code&gt;App&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;Let's &lt;code&gt;console.log("")&lt;/code&gt; because we want to see if we clicked the button, it will appear in the console &lt;code&gt;"Toggle Clicked!"&lt;/code&gt; to show that is working.&lt;/p&gt;

&lt;p&gt;Let's have an arrow function for &lt;code&gt;toggle()&lt;/code&gt;. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toggle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Toggle Clicked!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, this is the way how we can handle events in React.&lt;/p&gt;

&lt;h1&gt;
  
  
  Change Toggle Explained
&lt;/h1&gt;

&lt;p&gt;Now, what I am going to do in &lt;code&gt;App.js&lt;/code&gt; the file is whenever I clicked the &lt;code&gt;toggle&lt;/code&gt; function, I want to change the &lt;code&gt;[flag]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If I want to set the state of the &lt;code&gt;flag&lt;/code&gt;, we will have a &lt;code&gt;setFlag&lt;/code&gt; and with this &lt;code&gt;setFlag&lt;/code&gt;, we will have just have to toggle the flag value &lt;code&gt;setFlag(!flag)&lt;/code&gt;. Like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toggle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button toggle clicked!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, if the flag value is &lt;code&gt;true&lt;/code&gt;, we going to set it as &lt;code&gt;false&lt;/code&gt;, and if the flag value is &lt;code&gt;false&lt;/code&gt;, it is going to be set to &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;!flag === false&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, let's have here in the App component were inside the return of the &lt;code&gt;&amp;lt;Clock /&amp;gt;&lt;/code&gt; tag, I am going to write a conditional component with it.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;Clock&lt;/code&gt; component, let's write a condition and it would be that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If I have a &lt;code&gt;flag&lt;/code&gt; as &lt;code&gt;true&lt;/code&gt;, then in that case I want to show the component which is the &lt;code&gt;Clock&lt;/code&gt; component.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But if it is &lt;code&gt;false&lt;/code&gt;, then in that case I don't want to show anything. Instead, I want to show the &lt;code&gt;"No Clock component"&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No Clock component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, I will save it is like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toggle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Toggle Clicked!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="nf"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="p"&gt;};&lt;/span&gt;

 &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
     &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;toggle&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Toggle Clock Component&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Clock&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No Clock component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
 &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Clean Up of Interval
&lt;/h1&gt;

&lt;p&gt;Now, what you will see if I clicked the &lt;code&gt;toggle&lt;/code&gt; the function then, you will see that we have that &lt;code&gt;"No Clock component"&lt;/code&gt; on the screen.&lt;/p&gt;

&lt;p&gt;Then, if I click the button again, you will see that we have back again a &lt;code&gt;Clock&lt;/code&gt; component showing the clock ticking on the screen.&lt;/p&gt;

&lt;p&gt;But, here what I want to show you is something from my &lt;a href="https://amirahnasihah.hashnode.dev/react-tutorial-beginner-usestate-and-useeffect-with-example-code" rel="noopener noreferrer"&gt;previous post&lt;/a&gt; which is the lifecycle method.&lt;/p&gt;

&lt;p&gt;So, what happened is whenever we clicked on the toggle &lt;code&gt;Clock&lt;/code&gt; component, and whenever this is &lt;code&gt;false&lt;/code&gt;, we don't see this &lt;code&gt;Clock&lt;/code&gt; component. In that case, we want to do a cleanup.&lt;/p&gt;

&lt;p&gt;And you see that now I am going to click on the toggle &lt;code&gt;"No Clock component"&lt;/code&gt; and you will see that we have cleaned up the interval &lt;code&gt;cleanup of interval&lt;/code&gt; in the console.&lt;/p&gt;

&lt;p&gt;And we have also clicked on the toggle and now you will see that we don't have any &lt;code&gt;setInterval&lt;/code&gt; running.&lt;/p&gt;

&lt;p&gt;And as soon as I click on it again, you will see the component is rendered again and you will see that again we have the component mounted or unmounted if I click on it, it will get a toggle &lt;code&gt;"No Clock component"&lt;/code&gt; and cleanup will run.&lt;/p&gt;

&lt;p&gt;So, that is how you will handle the events in your react application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s2"&gt;`Clock`&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/`Clock`&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toggle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Toggle Clicked!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="nf"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="p"&gt;};&lt;/span&gt;

 &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Toggle&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;     &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No Clock component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Anonymous Arrow Function
&lt;/h1&gt;

&lt;p&gt;Here in &lt;code&gt;App.js&lt;/code&gt;, we can also do one more thing instead of calling this &lt;code&gt;toggle&lt;/code&gt; function as usual:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toggle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also return an &lt;strong&gt;Anonymous Arrow Function&lt;/strong&gt; in the &lt;code&gt;onClick&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Observe the code below to see the differences in this method code.&lt;/p&gt;

&lt;p&gt;And in this function, what I am going to have is a &lt;code&gt;setClick()&lt;/code&gt; and I am going to toggle the &lt;code&gt;flag&lt;/code&gt; value and this will work as it is.&lt;/p&gt;

&lt;p&gt;So, if you click on the button, it still works as it is.&lt;/p&gt;

&lt;p&gt;So, in this way, you can handle the events in React application.&lt;/p&gt;

&lt;h2&gt;
  
  
  First Method
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toggle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Toggle Clicked!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="nf"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="p"&gt;};&lt;/span&gt;

 &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;toggle&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Toggle&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;     &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No Clock component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at the differences between the &lt;code&gt;onClick&lt;/code&gt; first method and the second method. In the second method, we removed the &lt;code&gt;toggle&lt;/code&gt; function and used an anonymous arrow function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Second Method (Anonymous Arrow Function)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
     &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Toggle Clock Component&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Clock&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No Clock component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
 &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Full Code
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/Clock&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toggle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Toggle Clicked!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Time Now:&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;toggle&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Toggle Clock Component&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;br&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Clock&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No Clock component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Clock.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTime&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en-GB&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;showTime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;component mounted or updated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cleanup of interval&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;showTime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en-GB&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Share This Tutorial
&lt;/h1&gt;

&lt;p&gt;👉 Please share my posts with the community at &lt;a href="http://daily.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;daily.dev&lt;/strong&gt;&lt;/a&gt; / social media by adding the article's URL to the feed. By adding my article's URL to the feed, I can share my insights and knowledge with other tech enthusiasts and contribute to the passionate community.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cheers✨&lt;/code&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How To Use Components &amp; Props in React with Example Code</title>
      <dc:creator>Amirah Nasihah</dc:creator>
      <pubDate>Sun, 17 May 2026 14:16:15 +0000</pubDate>
      <link>https://dev.to/amirahnasihah/how-to-use-components-props-in-react-with-example-code-6be</link>
      <guid>https://dev.to/amirahnasihah/how-to-use-components-props-in-react-with-example-code-6be</guid>
      <description>&lt;h1&gt;
  
  
  How To Use Components &amp;amp; Props in React with Example Code
&lt;/h1&gt;

&lt;p&gt;In this tutorial, I'm going to give you a step-by-step guide on understanding one of the main concepts of React, which is components and props in React applications, with example code.&lt;/p&gt;

&lt;h1&gt;
  
  
  Components &amp;amp; Props in React
&lt;/h1&gt;

&lt;p&gt;Now, let's understand the Components and the Props in React.&lt;/p&gt;

&lt;p&gt;You can think of a component as a simple JavaScript function, and it will take the props as an argument, and you can use those props in your component. And then that component is going to return a &lt;code&gt;JSX&lt;/code&gt; which is going to display it on the screen.&lt;/p&gt;

&lt;h1&gt;
  
  
  The First Step
&lt;/h1&gt;

&lt;p&gt;To begin, you can create react app using the command line or any code editor (e.g., VSCode). You can also try using &lt;a href="https://codesandbox.io/" rel="noopener noreferrer"&gt;CodeSandbox&lt;/a&gt; for an online code editor that is simple to use and allows you to deploy your code.&lt;/p&gt;

&lt;p&gt;To have a better understanding of how components and properties are used in React, read through this tutorial and follow along several times.&lt;/p&gt;

&lt;h1&gt;
  
  
  Code Output
&lt;/h1&gt;

&lt;p&gt;This is the result that you will achieve once you have finished the tutorial. My suggestion is to read this guide closely, then go through it again one step at a time using code editor and repeat the process until you can do it by yourself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fplay-codesandbox.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fplay-codesandbox.svg" alt="Edit components-and-props-hello-diff-users" width="165" height="32"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  A Simple JavaScript Function
&lt;/h1&gt;

&lt;p&gt;So, let's do this with an example. So, what I'm going to do here in &lt;code&gt;App.js&lt;/code&gt; file is that I'm just simply roll back everything. We don't want anything on our screen.&lt;/p&gt;

&lt;p&gt;Now, if we go to &lt;code&gt;App.js&lt;/code&gt; file and in this App component, you will see that this &lt;code&gt;App.js&lt;/code&gt; is a simple function of JavaScript.&lt;/p&gt;

&lt;p&gt;So, what I will do is I will have this constant &lt;code&gt;userInfo&lt;/code&gt; and let's also have a string here inside the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; tag. Let's have a string of &lt;code&gt;Hello World&lt;/code&gt;. Let's do it like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ludwig&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Beethoven&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
 &lt;span class="p"&gt;};&lt;/span&gt;

 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello World&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, and we have this &lt;code&gt;App&lt;/code&gt; as a function, a simple javascript function, which is actually returning a &lt;code&gt;JSX&lt;/code&gt;, and the &lt;code&gt;JSX&lt;/code&gt; is the &lt;code&gt;Hello World&lt;/code&gt; which is being displayed on the screen.&lt;/p&gt;

&lt;h1&gt;
  
  
  Understanding Components &amp;amp; Props
&lt;/h1&gt;

&lt;p&gt;So, this &lt;code&gt;App&lt;/code&gt; is called an &lt;code&gt;App component&lt;/code&gt; and in React, each of the component you can think of that you want to split your UI into different components.&lt;/p&gt;

&lt;p&gt;And each of the component will have it's data in the form of a &lt;code&gt;prop&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's take an example. So, what I'm going to do is I'm going to go here in &lt;code&gt;src folder&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And then, I will create a folder named &lt;code&gt;components&lt;/code&gt; inside the &lt;code&gt;src folder&lt;/code&gt; where in here I can create my own components.&lt;/p&gt;

&lt;p&gt;Then, inside the &lt;code&gt;components folder&lt;/code&gt; I will have a component called &lt;code&gt;Welcome.js&lt;/code&gt;. Let's create a &lt;code&gt;.js&lt;/code&gt; file which contained a component call &lt;code&gt;Welcome.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Alright, and you should make sure that whenever you create a component, you should always start a component with a capital letter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Welcome.js&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here in the &lt;code&gt;Welcome.js&lt;/code&gt; file, I want to create a component.&lt;/p&gt;

&lt;p&gt;So, what I will do here is that I'm going to create a function and the name of the function will be &lt;code&gt;Welcome&lt;/code&gt;. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, then at the end of the &lt;code&gt;Welcome&lt;/code&gt; function, I will have to export this Welcome component.&lt;/p&gt;

&lt;p&gt;So, I will have a:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, this Welcome component should return some kind of a &lt;code&gt;JSX&lt;/code&gt;. Let's return a JSX where I'm going to return &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; tag and I will have a &lt;code&gt;Hello from Welcome Component&lt;/code&gt;. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello from Welcome Component&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, I will save it.&lt;/p&gt;

&lt;p&gt;As soon as we save it, you will not see anything on the screen because we have created a component but we are not using this component.&lt;/p&gt;

&lt;h1&gt;
  
  
  To Use The Component
&lt;/h1&gt;

&lt;p&gt;In order to use the Welcome component, we need to import the component.&lt;/p&gt;

&lt;p&gt;So, let's go to the &lt;code&gt;App.js&lt;/code&gt; file and at the top of the code, I will import the Welcome component.&lt;/p&gt;

&lt;p&gt;Let's import the Welcome component like so:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/Welcome&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, now the Welcome component is imported.&lt;/p&gt;

&lt;p&gt;And then, if you want to use this Welcome component, what you need to do here is remove this &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; element. From this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello World&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, what I'm going to have is an empty fragment &lt;code&gt;&amp;lt;&amp;gt; &amp;lt;/&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, let's have an empty fragment and inside this empty fragment, I will have the Welcome component. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;;
  &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, in this way, you can use the component and let's save it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An empty tag &lt;code&gt;&amp;lt;&amp;gt;&amp;lt;/&amp;gt;&lt;/code&gt; is a shortcut syntax for fragment components. While it is available in the JSX syntax, it is not part of the HTML standard and hence is not natively supported by browsers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, you will see that as soon as we save it, you will see on the screen that &lt;code&gt;Hello from Welcome Component&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, what I want is I want to pass the props to this component which is the App component. I want to pass some information. So, I will go here in App component on return &lt;code&gt;&amp;lt;Welcome /&amp;gt;&lt;/code&gt; and I'm going to pass the &lt;code&gt;user&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The prop name is &lt;code&gt;user&lt;/code&gt; and I'm going to have the &lt;code&gt;userInfo&lt;/code&gt;. It will be like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ludwig&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Beethoven&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
 &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;userInfo&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, I will pass the &lt;code&gt;userInfo&lt;/code&gt; and this &lt;code&gt;userInfo&lt;/code&gt;, I will save it.&lt;/p&gt;

&lt;p&gt;Now, I have passed the props.&lt;/p&gt;

&lt;h1&gt;
  
  
  To Use Props in React
&lt;/h1&gt;

&lt;p&gt;Now, I want to use this &lt;code&gt;props&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To use the props, you will need to go here in Welcome component, and whenever you pass a prop in a component, you will take it as an argument.&lt;/p&gt;

&lt;p&gt;Like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Welcome.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, let's do a simple &lt;code&gt;console.log&lt;/code&gt; on the Welcome component.&lt;/p&gt;

&lt;p&gt;I'm going to have a &lt;code&gt;console.log&lt;/code&gt; of the props because I want to see what props that I have. Then, I will save it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And next, let's have the inspect element.&lt;/p&gt;

&lt;p&gt;I will go to the console. And in the console, you will see that I have an &lt;code&gt;Object&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If I expand this Object, I will see that I have the information of the &lt;code&gt;user&lt;/code&gt; and the &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, if I want to use it, what I can do is I can simply have here the &lt;code&gt;props.name&lt;/code&gt;. Like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Welcome.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; from Welcome Component&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, and I will save it now.&lt;/p&gt;

&lt;p&gt;As soon as I save it, you will see that I don't see name displayed on the screen.&lt;/p&gt;

&lt;p&gt;That is because I made a mistake where it supposed to be &lt;code&gt;props.user.firstName&lt;/code&gt;. Let's have it like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Welcome.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; from Welcome Component&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, and when I save it we will see the &lt;code&gt;Hello Ludwig from Welcome Component&lt;/code&gt; on the screen.&lt;/p&gt;

&lt;p&gt;Hence, in this way, you can actually create a component and you can pass the prop.&lt;/p&gt;

&lt;h1&gt;
  
  
  Reusable Component
&lt;/h1&gt;

&lt;p&gt;Now, I want to show you that how you can make this on it as a reusable component.&lt;/p&gt;

&lt;p&gt;So, let's remove this here inside the &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; tag and I'm going to simply have the &lt;code&gt;name&lt;/code&gt; here again. Like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Welcome.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; from Welcome Component&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, and what I want is that this Welcome component to be repeated three (3) times with a different name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I will go to the &lt;code&gt;App.js&lt;/code&gt; file and I'm going to repeat this three times because I want to use this three times. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;userInfo&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;userInfo&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;userInfo&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, and let's change the information first.&lt;/p&gt;

&lt;p&gt;I'm going to pass the first information as &lt;code&gt;Ludwig&lt;/code&gt;. Next, I'm going to pass the second information as &lt;code&gt;Beethoven&lt;/code&gt; and for the third information, I will pass this as &lt;code&gt;Johann&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Alright, now I have the three users like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Ludwig"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Beethoven"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Johann"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see that we have three different users but, you will see that we don't have the username displayed on the screen.&lt;/p&gt;

&lt;p&gt;So, that means we made a mistake and the mistake is we are using &lt;code&gt;props.name&lt;/code&gt; in Welcome component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Welcome.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; from Welcome Component&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem of our prop is the &lt;code&gt;name&lt;/code&gt;. I'm going to go and I will simply going to change this as &lt;code&gt;user&lt;/code&gt; and I will save it and you will see that we have that &lt;code&gt;Hello Ludwig from Welcome Component&lt;/code&gt; along with all of the other users been displayed on the screen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Welcome.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; from Welcome Component&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, try thinking and practice on how to display the &lt;code&gt;lastName&lt;/code&gt; on the screen.&lt;/p&gt;

&lt;h1&gt;
  
  
  Class Based Component
&lt;/h1&gt;

&lt;p&gt;So, this is how you can use the components and the props and this component is called a &lt;code&gt;function component&lt;/code&gt; because you are writing it as a JavaScript function.&lt;/p&gt;

&lt;p&gt;And another way of writing a component which is using a &lt;code&gt;class based component&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But, I'm not going to show the &lt;code&gt;class based component&lt;/code&gt; because the class based component is mostly used on the Legacy (outdated or obsolete) application and any modern React application is going to use the &lt;code&gt;function component&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, you can relate this article of components and props with my another &lt;a href="https://amirahnasihah.hashnode.dev/react-tutorial-beginner-usestate-and-useeffect-with-example-code" rel="noopener noreferrer"&gt;article about understanding the state and the lifecycle with examples&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Full Code
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;App.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/Welcome&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ludwig&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Beethoven&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Ludwig"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Beethoven"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Johann"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Welcome.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; from Welcome Component&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Share This Tutorial
&lt;/h1&gt;

&lt;p&gt;👉 Please share my posts with the community at &lt;a href="http://daily.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;daily.dev&lt;/strong&gt;&lt;/a&gt; / social media by adding the article's URL to the feed. By adding my article's URL to the feed, I can share my insights and knowledge with other tech enthusiasts and contribute to the passionate community.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cheers✨&lt;/code&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Prevent Memory Leakage with the `useEffect` Hook - React</title>
      <dc:creator>Amirah Nasihah</dc:creator>
      <pubDate>Sun, 17 May 2026 14:16:13 +0000</pubDate>
      <link>https://dev.to/amirahnasihah/prevent-memory-leakage-with-the-useeffect-hook-react-3ji8</link>
      <guid>https://dev.to/amirahnasihah/prevent-memory-leakage-with-the-useeffect-hook-react-3ji8</guid>
      <description>&lt;h1&gt;
  
  
  Prevent Memory Leakage with the &lt;code&gt;useEffect&lt;/code&gt; Hook - React
&lt;/h1&gt;

&lt;p&gt;Beginners may be concerned about having &lt;code&gt;useEffect&lt;/code&gt; in the code in my previous tutorial post about useState and useEffect. You can check out my step-by-step &lt;a href="https://amirahnasihah.hashnode.dev/react-tutorial-beginner-usestate-and-useeffect-with-example-code" rel="noopener noreferrer"&gt;React Tutorial Beginner - useState and useEffect with Example Code&lt;/a&gt;. I will explain below why we need a &lt;code&gt;useEffect&lt;/code&gt; in our code in this post.&lt;/p&gt;

&lt;h1&gt;
  
  
  Memory Leakage in React Applications
&lt;/h1&gt;

&lt;p&gt;Create a &lt;code&gt;useEffect&lt;/code&gt; hook to &lt;em&gt;Clock.js&lt;/em&gt; that clears the &lt;code&gt;setInterval&lt;/code&gt; every time the Clock component is updated. Other than that, you'd have a lot of "intervals" that may result in memory leaks.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you're not sure what memory leaking is, do a Google search for “memory leakage”.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  What is the &lt;code&gt;useEffect&lt;/code&gt; Hook?
&lt;/h1&gt;

&lt;p&gt;When a component is updated or re-rendered, the &lt;code&gt;useEffect&lt;/code&gt; hook is invoked or called. You can use it to perform tasks before the component is loaded, updated, or refreshed.&lt;/p&gt;

&lt;h1&gt;
  
  
  Simplest Use
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;useEffect&lt;/code&gt; hook is used in the simplest possible way in the example below. It basically implies that the page title will be modified to &lt;code&gt;"You clicked X times"&lt;/code&gt; every time the component is refreshed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Update the document title using the browser API&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You clicked ${count} times&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only refresh when a certain variable changes.&lt;/p&gt;

&lt;p&gt;In this example below, you can see that we have added &lt;code&gt;[time]&lt;/code&gt; as the second parameter to the &lt;code&gt;useEffect&lt;/code&gt; hook. That is, this &lt;code&gt;useEffect&lt;/code&gt; hook would only run when the component is updated or refreshed due to an update in the time variable. This also applies to the first load.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Cleaning Up
&lt;/h1&gt;

&lt;p&gt;Sometimes the action you perform in the &lt;code&gt;useEffect&lt;/code&gt; hook will run for a long time or even forever (e.g., refreshing the clock every second with &lt;code&gt;setInterval&lt;/code&gt;). In this situation, make sure that when React refreshes the component, it cleans up the “long-running tasks”. Otherwise, you'd be running a slew of "long-running tasks" in the background, using up your whole computer's RAM.&lt;/p&gt;

&lt;p&gt;To do this, you must return a "clean up function" from the &lt;code&gt;useEffect&lt;/code&gt; hook. React will use that function to clean up the long-running task when the component is refreshed. If you put a long-running task in the &lt;code&gt;useEffect&lt;/code&gt; hook, you should &lt;strong&gt;always&lt;/strong&gt; include a clean up function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, you can see that we returned an arrow function called &lt;code&gt;clearInterval&lt;/code&gt; to handle the cleanup for us.&lt;/p&gt;

&lt;p&gt;Don't be too worried about this if you're not quite sure. It is rarely used in actual code.&lt;/p&gt;




&lt;h1&gt;
  
  
  Share This Tutorial
&lt;/h1&gt;

&lt;p&gt;👉 Please share my posts with the community at &lt;a href="http://daily.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;daily.dev&lt;/strong&gt;&lt;/a&gt; / social media by adding the article's URL to the feed. By adding my article's URL to the feed, I can share my insights and knowledge with other tech enthusiasts and contribute to the passionate community.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cheers✨&lt;/code&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>performance</category>
      <category>react</category>
    </item>
    <item>
      <title>React Tutorial Beginner - `useState` and `useEffect` with Example Code</title>
      <dc:creator>Amirah Nasihah</dc:creator>
      <pubDate>Sun, 17 May 2026 14:16:11 +0000</pubDate>
      <link>https://dev.to/amirahnasihah/react-tutorial-beginner-usestate-and-useeffect-with-example-code-2l06</link>
      <guid>https://dev.to/amirahnasihah/react-tutorial-beginner-usestate-and-useeffect-with-example-code-2l06</guid>
      <description>&lt;h1&gt;
  
  
  React Tutorial Beginner - &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt; with Example Code
&lt;/h1&gt;

&lt;p&gt;In this tutorial, I'm going to give you a step-by-step guide on understanding the main concepts of React which introduce the concept of state and lifecycle in a React component.&lt;/p&gt;

&lt;h1&gt;
  
  
  State is Memory of a Component
&lt;/h1&gt;

&lt;p&gt;Let's have a brief on what &lt;code&gt;State&lt;/code&gt; is.&lt;/p&gt;

&lt;p&gt;As a result of an interaction, components frequently need to update what's on the screen. Typing into the form updates the input box, clicking "next" on an image carousel changes the picture displayed and clicking "purchase" adds a product to the shopping cart. Components must "remember" information such as the current input value, the current picture, and the shopping cart. This type of component-specific memory is referred to as a state in React.&lt;/p&gt;

&lt;h1&gt;
  
  
  Full Tutorial Code
&lt;/h1&gt;

&lt;p&gt;These are the code that you will achieve once you are done with the tutorial. My advice is, to read it until finished and start doing again using the code editor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTime&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;component mounted or updated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cleanup of Interval&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;en-GB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="nf"&gt;setTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  First Step
&lt;/h1&gt;

&lt;p&gt;To begin, you can start creating your own react app using the command line or can directly go to &lt;a href="https://codesandbox.io/" rel="noopener noreferrer"&gt;CodeSandbox&lt;/a&gt; if you want to skip using the command line which is faster. CodeSandbox is an online code editor and prototype tool that speeds up the creation and sharing of web apps where you can directly deploy your app without any hustle.&lt;/p&gt;

&lt;p&gt;First, create a new folder inside the &lt;code&gt;src&lt;/code&gt; folder named &lt;code&gt;components&lt;/code&gt;. Then, create a JavaScript file called &lt;code&gt;Clock.js&lt;/code&gt; which in React is the component of &lt;code&gt;Clock&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Create a function &lt;code&gt;Clock&lt;/code&gt; Component.&lt;/p&gt;

&lt;p&gt;Export this component by typing &lt;code&gt;export default Clock&lt;/code&gt; below the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's create another function in this component, which will be the &lt;code&gt;showDate()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;After have &lt;code&gt;showDate()&lt;/code&gt;, we'll have a &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; tag to make it large or other tags you prefer and this &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; will have the date of &lt;code&gt;{new Date().toString()}&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we call this function with the &lt;code&gt;setInterval()&lt;/code&gt;. And have the &lt;code&gt;setInterval&lt;/code&gt; as in every one second. Let's call the function first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this &lt;code&gt;showDate()&lt;/code&gt; function will be called in every one second.&lt;/p&gt;

&lt;p&gt;Alright, and another thing going do is to have also &lt;code&gt;console.log()&lt;/code&gt; above the return method. We &lt;code&gt;console.log()&lt;/code&gt; the &lt;code&gt;Date&lt;/code&gt; function to see whether its running or not in our console. Like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And have to also return a JSX because every component returns a JSX.&lt;/p&gt;

&lt;p&gt;Return a JSX below the &lt;code&gt;setInterval()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Type &lt;code&gt;return (&amp;lt;div&amp;gt; &amp;lt;/div&amp;gt;)&lt;/code&gt; as below and let's call the &lt;code&gt;showDate()&lt;/code&gt; function inside the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; JSX.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use an empty fragments &lt;code&gt;&amp;lt;&amp;gt; &amp;lt;/&amp;gt;&lt;/code&gt; to display the JSX like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="c1"&gt;// empty fragment&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="c1"&gt;// empty fragment&lt;/span&gt;
 &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are not seeing anything happen on your screen, go to &lt;code&gt;App.js&lt;/code&gt; and import our Clock Component, then use the Clock Component by adding the &lt;code&gt;&amp;lt;Clock /&amp;gt;&lt;/code&gt;. Like this:&lt;/p&gt;

&lt;p&gt;App.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/Clock&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;use&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we will see what is actually happening when we use the Clock component. We see the time and here in the Clock component, you will see that we have a &lt;code&gt;setInterval&lt;/code&gt; which is being called every one second.&lt;/p&gt;

&lt;p&gt;So, if I go to the inspect element (&lt;code&gt;F12&lt;/code&gt; hotkey) and if I go to console, then in the console you will see that the time is getting updated every second. This is what we will achieve so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Time not updated on the screen
&lt;/h1&gt;

&lt;p&gt;But then, on the screen this time not getting updated every second. Only inside the console does it get updated every second.&lt;/p&gt;

&lt;p&gt;And that is where we have to use the &lt;code&gt;State&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A state will be a local state of a component. So, whenever a State is changed, a component will re-render and it will render the updated &lt;code&gt;JSX&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, what we can do is, we need to import a State.&lt;/p&gt;

&lt;p&gt;What to do is in the &lt;code&gt;Clock.js&lt;/code&gt; Component. Let's have an &lt;code&gt;import React from 'react';&lt;/code&gt; at the top of the &lt;code&gt;Clock()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;And in order to use a State, there is a hook called &lt;code&gt;useState&lt;/code&gt;. To use the &lt;code&gt;useState&lt;/code&gt; hook, you need to import the state variable from 'react'. Like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, now let's create a &lt;code&gt;constant&lt;/code&gt;. To use a State in a function component, you need to create a constant and you have to use an array &lt;code&gt;[]&lt;/code&gt; like structure, and then you will have a &lt;code&gt;useState()&lt;/code&gt;. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here you need to give the initial value of your state =&amp;gt; &lt;code&gt;useState(initial value)&lt;/code&gt;. So, my initial value will be the same which is the &lt;code&gt;Date()&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Put inside &lt;code&gt;useState()&lt;/code&gt;, and here I will have the &lt;code&gt;time&lt;/code&gt; and I will have a &lt;code&gt;setTime&lt;/code&gt;. Whenever you use a &lt;code&gt;useState()&lt;/code&gt;, it actually gives you a time variable and it also gives a function to update that variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTime&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now, we are going to change line &lt;code&gt;return &amp;lt;div&amp;gt;{showDate()}&amp;lt;/div&amp;gt;;&lt;/code&gt; and have a &lt;code&gt;{time}&lt;/code&gt; here and save it. Then start seeing a time on the screen and this is the time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, what I'm going to do is I need to update that time.&lt;/p&gt;

&lt;p&gt;So whenever this &lt;code&gt;setInterval&lt;/code&gt; makes a call to the &lt;code&gt;showDate()&lt;/code&gt; in every one second instead of, returning this &lt;code&gt;return &amp;lt;h1&amp;gt;{new Date().toString()}&amp;lt;/h1&amp;gt;;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, I'm going to update my state.&lt;/p&gt;

&lt;h1&gt;
  
  
  To Update the State
&lt;/h1&gt;

&lt;p&gt;So want to update my state, I'm going to do a &lt;code&gt;setTime()&lt;/code&gt; and this &lt;code&gt;setTime()&lt;/code&gt; is going to update the state.&lt;/p&gt;

&lt;p&gt;So, let's type &lt;code&gt;setTime(new Date().toString())&lt;/code&gt; and save it.&lt;/p&gt;

&lt;p&gt;So, every second my &lt;code&gt;showDate()&lt;/code&gt; function is being called &lt;code&gt;function showDate() {&lt;/code&gt; and whenever it is called I changed the state variable.&lt;/p&gt;

&lt;p&gt;So, you change the state variable with the help of &lt;code&gt;set&lt;/code&gt; function; &lt;code&gt;[setTime] = ..&lt;/code&gt;. This is a set function &lt;code&gt;setTime(new Date().toString());&lt;/code&gt; and as soon as I do a &lt;code&gt;setTime()&lt;/code&gt; with a new updated time &lt;code&gt;new Date().toString()&lt;/code&gt;, so my state &lt;code&gt;useState()&lt;/code&gt; gets changed and whenever the states get change, the component is re-rendered and that's where we will see that the screen is now updating.&lt;/p&gt;

&lt;p&gt;We can see now on the screen the time is running every second and also from the console.&lt;/p&gt;

&lt;p&gt;If error occurs, make sure that the &lt;code&gt;showDate()&lt;/code&gt; is inside the &lt;code&gt;Clock()&lt;/code&gt; function. Like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTick&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
 &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
   &lt;span class="nf"&gt;setTick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;showDate()&lt;/code&gt; function works as updating in every one second, and we use &lt;code&gt;showDate()&lt;/code&gt; function inside the &lt;code&gt;Clock&lt;/code&gt; function to update the time every one second, to do that, we use state variable which is &lt;code&gt;setTime(new Date().toString())&lt;/code&gt;. Earlier in &lt;code&gt;showDate()&lt;/code&gt; function we put &lt;code&gt;return &amp;lt;h6&amp;gt;{date}&amp;lt;/h6&amp;gt;;&lt;/code&gt; because whenever you use a &lt;code&gt;useState()&lt;/code&gt;, it actually gives you a time Variable and it also gives a function to update that variable which using &lt;code&gt;set&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Thats why we change from &lt;code&gt;return &amp;lt;h6&amp;gt;{date}&amp;lt;/h6&amp;gt;;&lt;/code&gt; to &lt;code&gt;setTime(new Date().toString())&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now on the screen, the time is running every one second and also from the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTime&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en-GB&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
 &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updateTime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="c1"&gt;// this function used to update the time every one second&lt;/span&gt;
 &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;updateTime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en-GB&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
   &lt;span class="nf"&gt;setTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en-GB&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Clock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;useState&lt;/code&gt; Hook
&lt;/h2&gt;

&lt;p&gt;Now, what is this &lt;code&gt;useState()&lt;/code&gt; is and what is this &lt;code&gt;[time]&lt;/code&gt; and &lt;code&gt;[setTime]&lt;/code&gt; is.&lt;/p&gt;

&lt;p&gt;Let's have a state, I'm gonna have a constant and now I'm going to create a state variable of &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, you will give it as a &lt;code&gt;name&lt;/code&gt;, and then whenever you want to update this name, you will update it with the help of a &lt;code&gt;setName&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, you can also do the same thing whenever you want to have a flag. Alright, and now you want to update this flag value so you will have a &lt;code&gt;setFlag&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFlag&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, so you will use the &lt;code&gt;useState()&lt;/code&gt; as &lt;code&gt;const [flag, setFlag] = useState()&lt;/code&gt; and then you will set the initial value.&lt;/p&gt;

&lt;p&gt;So, my initial value of the flag is &lt;code&gt;true&lt;/code&gt;. so let's have a &lt;code&gt;useState(true)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this way, you will create and use the State.&lt;/p&gt;

&lt;h1&gt;
  
  
  Lifecycle using the &lt;code&gt;useEffect&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;Now, what is the lifecycle?&lt;/p&gt;

&lt;p&gt;So whenever you have a component, the component also has lifecycles.&lt;/p&gt;

&lt;p&gt;So the lifecycles will be that you want to execute something whenever your component is &lt;em&gt;mounted&lt;/em&gt; or whenever your component is first time rendered or you want to do something whenever your component is getting updated or whenever your component is destroyed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 Mounting is the phase in which our React component mounts on the DOM (i.e., is created and inserted into the DOM). This method is called just before a component mounts on the DOM or the render method is called. After this method, the component gets mounted.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example, the Clock component is inserted into the &lt;code&gt;DOM&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We use lifecycle because we want the clock keeps updating. So, need to use the &lt;code&gt;useEffect()&lt;/code&gt; function&lt;/p&gt;

&lt;p&gt;So, in those cases, you will use a lifecycle.&lt;/p&gt;

&lt;p&gt;To use a life cycle method in the function component, we make use of a &lt;code&gt;useEffect&lt;/code&gt; hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, this way I will use an &lt;code&gt;useEffect&lt;/code&gt; hook. And now what I'm going to do here is that I'm going to write a &lt;code&gt;useEffect&lt;/code&gt; and this &lt;code&gt;useEffect&lt;/code&gt; will have two arguments, &lt;code&gt;useEffect(args1, args2)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The first argument will be a &lt;code&gt;function&lt;/code&gt; or a side effect that you want to run and the second argument will be a dependency array. I will use an arrow function as the first argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the second argument will be the dependency array. So this dependency array will depend on these State variables.&lt;/p&gt;

&lt;p&gt;So, now what I'm going to do here is that let's have a &lt;code&gt;console.log()&lt;/code&gt; and I'm going to have here a string of &lt;code&gt;"component mounted or updated"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Alright, and remove the dependency array and save it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;component mounted or updated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'll save it and now let's go to the inspect element and I will go to the console and here I will just remove this function because we don't want and it's polluting our console. Let's comment out &lt;code&gt;console.log(new Date().toString());&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Alright, now I will refresh my page. And you will see that as soon as I refresh my page, My component is mounted and my state is also getting updated.&lt;/p&gt;

&lt;p&gt;So, the state is getting updated because this state variable is getting updated.&lt;/p&gt;

&lt;p&gt;So, if I don't want that this gets updated every time, what I can do here is that I have a dependency array &lt;code&gt;[]&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;component mounted or updated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if I give it a dependency in this array of a state where able then in that case the &lt;code&gt;useEffect()&lt;/code&gt; will run only if the state variable changes.&lt;/p&gt;

&lt;p&gt;So, what I'm going to do is I'm going to have a flag here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, save it, and now will refresh the page.&lt;/p&gt;

&lt;p&gt;So, you now will see that even though your time is getting changed in every one second, your &lt;code&gt;useEffect()&lt;/code&gt; &lt;code&gt;useEffect( ... ;}, [time]);&lt;/code&gt; is not running because your &lt;code&gt;useEffect()&lt;/code&gt; is now dependent on another state variable which is a &lt;code&gt;time&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, that means whenever a &lt;code&gt;time&lt;/code&gt; is changed in that case, the &lt;code&gt;useEffect()&lt;/code&gt; will run.&lt;/p&gt;

&lt;p&gt;So, if the flag is changing from &lt;code&gt;true&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt;, then you will see that the &lt;code&gt;useEffect&lt;/code&gt; will run if I change this flag to &lt;code&gt;true&lt;/code&gt; again, then you will see that the &lt;code&gt;useEffect&lt;/code&gt; will run.&lt;/p&gt;

&lt;p&gt;So this way this is a lifecycle of a component, whenever a component is mounted or whenever a component is updated. you want to run some side effects.&lt;/p&gt;

&lt;p&gt;Now, there is a scenario that whenever a component is destroyed you want to run effect of unmounting that whenever the component is unmounted you want to do something.&lt;/p&gt;

&lt;p&gt;So, in that case, what we can do is that let's have a constant and gonna have an interval and I will remove/cut the &lt;code&gt;setInterval()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So now I have the interval here and this interval depends on the time.&lt;/p&gt;

&lt;p&gt;So, I'm gonna copy this {time} and gonna have the {time} here at &lt;code&gt;useEffect&lt;/code&gt; dependency array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, you can see that every time this &lt;code&gt;showDate()&lt;/code&gt; function is running my component is getting updated.&lt;/p&gt;

&lt;p&gt;So, that's why we see a lot components updated on console logs.&lt;/p&gt;

&lt;p&gt;Now, let's assume that you have a subscription to this interval.&lt;/p&gt;

&lt;p&gt;Now, you want to clear this subscription whenever your component is unmounted or whenever your component is not visible on your screen.&lt;/p&gt;

&lt;p&gt;So what we can do here, inside this &lt;code&gt;useEffect()&lt;/code&gt; function is that we can have a clean function. Which will have a return and this will be an arrow function. Alright and here, inside this arrow function, you can do your cleanup.&lt;/p&gt;

&lt;p&gt;So, let me have a &lt;code&gt;console.log("cleanup of Interval")&lt;/code&gt; and below gonna have the &lt;code&gt;clearInterval&lt;/code&gt; method. And let gonna clear this interval. I'm going to add the constant interval into the &lt;code&gt;clearInterval(interval)&lt;/code&gt;. Like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cleanup every one second&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;showDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
   &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now see that whenever a component is mounted. the first thing that will happen is the &lt;code&gt;cleanup function&lt;/code&gt; will run whenever this component is mounted or whenever a component is unmounted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;component keep mounted or updated&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cleanup of Interval&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updateTime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Dependency Array
&lt;/h1&gt;

&lt;p&gt;If we remove the dependency, which is [time], in the console will look like this, not update the interval.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;component keep mounted or updated
cleanup of Interval
component keep mounted or updated
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is not running because the &lt;code&gt;useEffect()&lt;/code&gt; is now dependent on another state variable which is a &lt;code&gt;time&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But if we put dependency array [time], then the console will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;cleanup of Interval
component keep mounted or updated
cleanup of Interval
component keep mounted or updated
cleanup of Interval
component keep mounted or updated
cleanup of Interval
component keep mounted or updated
cleanup of Interval
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in that case, the &lt;code&gt;useEffect()&lt;/code&gt; will run only if the state variable changes. The second argument is will be the dependency array. So, this dependency array will depend on these State variables.&lt;/p&gt;

&lt;p&gt;So, this is how you will do the State and the lifecycle in the React components.&lt;/p&gt;

&lt;h1&gt;
  
  
  CodeSandbox
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fplay-codesandbox.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodesandbox.io%2Fstatic%2Fimg%2Fplay-codesandbox.svg" alt="Edit state-and-lifecycle-ticking-clock" width="165" height="32"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Share This Tutorial
&lt;/h1&gt;

&lt;p&gt;👉 Please share my posts with the community at &lt;a href="http://daily.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;daily.dev&lt;/strong&gt;&lt;/a&gt; / social media by adding the article's URL to the feed. By adding my article's URL to the feed, I can share my insights and knowledge with other tech enthusiasts and contribute to the passionate community.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cheers✨&lt;/code&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Props vs State in ReactJS</title>
      <dc:creator>Amirah Nasihah</dc:creator>
      <pubDate>Sun, 17 May 2026 14:16:09 +0000</pubDate>
      <link>https://dev.to/amirahnasihah/props-vs-state-in-reactjs-52o1</link>
      <guid>https://dev.to/amirahnasihah/props-vs-state-in-reactjs-52o1</guid>
      <description>&lt;h1&gt;
  
  
  Props vs State in ReactJS
&lt;/h1&gt;

&lt;h1&gt;
  
  
  Props
&lt;/h1&gt;

&lt;p&gt;"Props" stands for "Properties." They are read-only components. It is an object which stores the value of the attributes of a tag and works similarly to the HTML attributes. It gives a way to pass data from one component to another component. It is similar to function arguments. Props are passed to the component in the same way as arguments are passed in a function.&lt;/p&gt;

&lt;p&gt;Props are immutable, so we cannot modify the props from inside the component. Inside the components, we can add attributes called props. These attributes are available in the component as &lt;code&gt;this.props&lt;/code&gt; and can be used to render dynamic data in our render method.&lt;/p&gt;

&lt;p&gt;When you need immutable data in the component, you have to add props to the &lt;code&gt;reactDom.render()&lt;/code&gt; method in the main.js file of your ReactJS project and use it inside the component in which you need it. It can be explained in the below example.&lt;/p&gt;

&lt;h1&gt;
  
  
  State
&lt;/h1&gt;

&lt;p&gt;The state is an updatable structure that is used to contain data or information about the component. The state of a component can change over time. The change in state over time can happen as a response to user action or a system event. A component with the state is known as a "stateful component." It is the heart of the react component that determines the behavior of the component and how it will render. They are also responsible for making a component dynamic and interactive.&lt;/p&gt;

&lt;p&gt;A state must be kept as simple as possible. It can be set by using the setState() method, and calling the setState() method triggers UI updates. A state represents the component's local state or information. It can only be accessed or modified inside the component or by the component directly. To set an initial state before any interaction occurs, we need to use the getInitialState() method.&lt;/p&gt;

&lt;p&gt;For example, if we have five components that need data or information from the state, then we need to create one container component that will hold the state for all of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining State
&lt;/h2&gt;

&lt;p&gt;To define a state, you have to first declare a default set of values for defining the component's initial state. To do this, add a class constructor which assigns an initial state using this.state. The 'this.state' property can be rendered inside the render() method.&lt;/p&gt;

&lt;p&gt;example&lt;/p&gt;

&lt;p&gt;The below sample code shows how we can create a stateful component using ES6 syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;import React, { Component } from "react";
class App extends React.Component {
    constructor() {
        super();
        this.state = { displayBio: true };
    }
    render() {
        const bio = this.state.displayBio ? (
            &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
                {" "}
                &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;
                        Skyeagle is one of the best Java training institute in
                        Noida, Delhi, Gurugram, Ghaziabad and Faridabad. We have
                        a team of experienced Java developers and trainers from
                        multinational companies to teach our campus students.
                    &lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;{" "}
            &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        ) : null;
        return (
            &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
                {" "}
                &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt; Welcome to &lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt; {bio}{" "}
            &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        );
    }
}
export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To set the state, it is required to call the &lt;code&gt;super()&lt;/code&gt; method in the constructor. It is because &lt;code&gt;this.state&lt;/code&gt; is uninitialized before the &lt;code&gt;super()&lt;/code&gt; method has been called.&lt;/p&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;h2&gt;
  
  
  Changing the State
&lt;/h2&gt;

&lt;p&gt;We can change the component state by using the &lt;code&gt;setState()&lt;/code&gt; method and passing a new state object as the argument. Now, create a new method &lt;code&gt;toggleDisplayBio()&lt;/code&gt; in the above example and bind this keyword to the &lt;code&gt;toggleDisplayBio()&lt;/code&gt; method otherwise we can't access this inside &lt;code&gt;toggleDisplayBio()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;this.toggleDisplayBio = this.toggleDisplayBio.bind(this);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example&lt;/p&gt;

&lt;p&gt;In this example, we are going to add a button to the render() method. Clicking on this button triggers the toggleDisplayBio() method which displays the desired output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;import React, { Component } from "react";

class App extends React.Component {
    constructor() {
        super();
        this.state = { displayBio: false };
        console.log("Component this", this);
        this.toggleDisplayBio = this.toggleDisplayBio.bind(this);
    }
    toggleDisplayBio() {
        this.setState({ displayBio: !this.state.displayBio });
    }&lt;span class="sb"&gt;

    render() {
        return (
            &amp;lt;div&amp;gt;
                {" "}
                &amp;lt;h1&amp;gt;Welcome to &amp;lt;/h1&amp;gt;{" "}
                {this.state.displayBio ? (
                    &amp;lt;div&amp;gt;
                        {" "}
                        &amp;lt;p&amp;gt;
                            &amp;lt;h4&amp;gt;
                                Skyeagle is one of the best Java training
                                institute in Noida, Delhi, Gurugram, Ghaziabad
                                and Faridabad. We have a team of experienced
                                Java developers and trainers from multinational
                                companies to teach our campus students.
                            &amp;lt;/h4&amp;gt;
                        &amp;lt;/p&amp;gt;{" "}
                        &amp;lt;button onClick={this.toggleDisplayBio}&amp;gt;
                            {" "}
                            Show Less{" "}
                        &amp;lt;/button&amp;gt;{" "}
                    &amp;lt;/div&amp;gt;
                ) : (
                    &amp;lt;div&amp;gt;
                        {" "}
                        &amp;lt;button onClick={this.toggleDisplayBio}&amp;gt;
                            {" "}
                            Read More{" "}
                        &amp;lt;/button&amp;gt;{" "}
                    &amp;lt;/div&amp;gt;
                )}{" "}
            &amp;lt;/div&amp;gt;
        );
    }
&lt;/span&gt;}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;When you click the Read More button, you will get the below output, and when you click the Show Less button, you will get the output as shown in the above image.&lt;/p&gt;

&lt;h1&gt;
  
  
  Combination of State and Props
&lt;/h1&gt;

&lt;p&gt;It is possible to combine both state and props in your app. You can set the state in the parent component and pass it in the child component using props. It can be shown in the below example.&lt;/p&gt;

&lt;p&gt;Example&lt;/p&gt;

&lt;p&gt;App.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = { name: "Skyeagle" };
    }
    render() {
        return (
            &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
                {" "}
                &lt;span class="nt"&gt;&amp;lt;JTP&lt;/span&gt; &lt;span class="na"&gt;jtpProp=&lt;/span&gt;&lt;span class="s"&gt;{this.state.name}&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;{" "}
            &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        );
    }
}
class JTP extends React.Component {
    render() {
        return (
            &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
                {" "}
                &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;State &amp;amp; Props Example&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;{" "}
                &lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;Welcome to {this.props.jtpProp}&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;{" "}
                &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
                    Skyeagle is one of the best Java training institute in
                    Noida, Delhi, Gurugram, Ghaziabad and Faridabad.
                &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;{" "}
            &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        );
    }
}
export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Main.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;import React from "react";
import ReactDOM from "react-dom";
import App from "./App.js";

ReactDOM.render(&lt;span class="nt"&gt;&amp;lt;App&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;, document.getElementById("app"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Differences between State vs. Props
&lt;/h1&gt;

&lt;p&gt;State&lt;/p&gt;

&lt;p&gt;The state is an updatable structure that is used to contain data or information about the component and can change over time. The change in state can happen as a response to user action or system event. It is the heart of the react component which determines the behaviour of the component and how it will render. A state must be kept as simple as possible. It represents the component's local state or information. It can only be accessed or modified inside the component or by the component directly.&lt;/p&gt;

&lt;p&gt;Props&lt;/p&gt;

&lt;p&gt;Props are read-only components. It is an object which stores the value of attributes of a tag and works similarly to the HTML attributes. It allows passing data from one component to another components. It is similar to function arguments and can be passed to the component the same way as arguments passed in a function. Props are immutable so we cannot modify the props from inside the component.&lt;/p&gt;




&lt;h1&gt;
  
  
  Share This Tutorial
&lt;/h1&gt;

&lt;p&gt;👉 Please share my posts with the community at &lt;a href="http://daily.dev" rel="noopener noreferrer"&gt;&lt;strong&gt;daily.dev&lt;/strong&gt;&lt;/a&gt; / social media by adding the article's URL to the feed. By adding my article's URL to the feed, I can share my insights and knowledge with other tech enthusiasts and contribute to the passionate community.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cheers✨&lt;/code&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>buildspace web3 demo showcase!</title>
      <dc:creator>Amirah Nasihah</dc:creator>
      <pubDate>Sun, 17 May 2026 14:16:07 +0000</pubDate>
      <link>https://dev.to/amirahnasihah/buildspace-web3-demo-showcase-mh</link>
      <guid>https://dev.to/amirahnasihah/buildspace-web3-demo-showcase-mh</guid>
      <description>&lt;h1&gt;
  
  
  buildspace web3 demo showcase!
&lt;/h1&gt;

&lt;p&gt;Last night's &lt;a href="https://buildspace.so/projects" rel="noopener noreferrer"&gt;buildspace&lt;/a&gt; demo day was a blast, as was go-karting around the map on gather.town dressed as a pumpkin. It's a massive web3 display!&lt;/p&gt;

&lt;p&gt;Over 150 fantastic web3 makers from the buildspace ecosystem and are giving them a shot to showcase what they've shipped. Got people building in GameFi, dev tools, education, social, and much more.&lt;/p&gt;

&lt;p&gt;buildspace N&amp;amp;W Demo Day happened on Saturday, August 6 at 11:00 PM Malaysia Time or August 6 at 11:00 AM ET. They had more than 500 people running around the demo map within that 4 hours.&lt;/p&gt;

&lt;p&gt;This was my second time attending a buildspace demo day, also explored a lot of crazy projects and met some fantastic people.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://rubix.bunsamosa.org" rel="noopener noreferrer"&gt;Rubix &lt;/a&gt;, &lt;a href="https://www.waitlistpanda.com/" rel="noopener noreferrer"&gt;WaitlistPanda&lt;/a&gt;, and &lt;a href="https://pipgig.com/signin" rel="noopener noreferrer"&gt;pipgig&lt;/a&gt; piqued my interest. Also, listen to and chat with them!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqiyr5enl87mk812yhsul.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqiyr5enl87mk812yhsul.png" alt="Screenshot (35).png" width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj2ct6a5626d7i40eu41j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj2ct6a5626d7i40eu41j.png" alt="Screenshot (38).png" width="800" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg9563iwipq524gesxza3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg9563iwipq524gesxza3.png" alt="Screenshot (42).png" width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjuowfy9xqes10lxvkmm0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjuowfy9xqes10lxvkmm0.png" alt="Screenshot (39).png" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>community</category>
      <category>showdev</category>
      <category>web3</category>
    </item>
    <item>
      <title>ReactJS - Introduction</title>
      <dc:creator>Amirah Nasihah</dc:creator>
      <pubDate>Sun, 17 May 2026 14:16:06 +0000</pubDate>
      <link>https://dev.to/amirahnasihah/reactjs-introduction-44m7</link>
      <guid>https://dev.to/amirahnasihah/reactjs-introduction-44m7</guid>
      <description>&lt;h1&gt;
  
  
  ReactJS - Introduction
&lt;/h1&gt;

&lt;h1&gt;
  
  
  What is ReactJS?
&lt;/h1&gt;

&lt;p&gt;➡️declarative, efficient, and flexible JavaScript library for building reusable UI components.&lt;/p&gt;

&lt;p&gt;➡️an open-source, component-based front-end library responsible only for the view layer of the application.&lt;/p&gt;

&lt;p&gt;➡️created by Jordan Walke, a software engineer at Facebook.&lt;/p&gt;

&lt;p&gt;➡️ was initially developed and maintained by Facebook and later used in its products like WhatsApp &amp;amp; Instagram before it was released to the public in the month of May 2013.&lt;/p&gt;

&lt;p&gt;➡️In MVC (model view controller) architecture, React is the 'V' which stands for view, whereas the architecture is provided by Redux or Flux.&lt;/p&gt;

&lt;h1&gt;
  
  
  How ReactJS Works
&lt;/h1&gt;

&lt;p&gt;💥A ReactJS application is made up of multiple components, each component responsible for outputting a small, reusable piece of HTML code.&lt;/p&gt;

&lt;p&gt;💥These Components can be nested with other components to allow complex applications to be built of simple building blocks.&lt;/p&gt;

&lt;p&gt;💥ReactJS uses a virtual DOM-based mechanism to fill data in HTML DOM.&lt;/p&gt;

&lt;p&gt;💥The virtual DOM works fast as it only changes individual DOM elements instead of reloading the complete DOM every time.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
