{"id":6903,"date":"2021-09-15T04:21:29","date_gmt":"2021-09-15T04:21:29","guid":{"rendered":"https:\/\/programmingfields.com\/?p=6903"},"modified":"2026-05-02T06:29:32","modified_gmt":"2026-05-02T06:29:32","slug":"how-to-use-useeffect-hook-in-react-functional-component","status":"publish","type":"post","link":"https:\/\/programmingfields.com\/how-to-use-useeffect-hook-in-react-functional-component\/","title":{"rendered":"How to Use Effect Hook in React Functional Component"},"content":{"rendered":"\n<p>The React hooks are a very powerful feature that allows us to hook into the React functionality. It is a JavaScript function that can be used or called inside the React functional component. You cannot use a hook inside the custom function in the component. So, it is necessary to call the hook function at the top level. In our last post, we have already seen the <a rel=\"noreferrer noopener\" href=\"https:\/\/programmingfields.com\/state-in-functional-component-in-react-using-usestate-hook\/\" target=\"_blank\">useState() hook<\/a> for setting and retrieving the state value(s). You can create the custom hook in React functional component also. That we will see in the upcoming post. Today, in this post, we will see React useEffect hook. The <strong>useEffect <\/strong>hook works to perform side effects in the functional component. That means when any certain changes have occurred in the component the <strong>React useEffect hook <\/strong>can be called. It updates the DOM immediately when something change occurred in the component.<\/p>\n\n\n\n<p>In the class component, React has the life cycle methods. Here, the <strong>useEffect<\/strong> is equivalent to <strong>componentDidMount()<\/strong>, <strong>componentDidUpdate()<\/strong>,<strong> componentWillUnmount()<\/strong> life cycle methods. Let&#8217;s see an example for a better understanding of using the <strong>useEffect hook<\/strong> in React js.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/programmingfields.com\/how-to-use-useeffect-hook-in-react-functional-component\/#Prerequisites\" >Prerequisites<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/programmingfields.com\/how-to-use-useeffect-hook-in-react-functional-component\/#What_is_useEffect_hook_in_React_js\" >What is useEffect hook in React js<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/programmingfields.com\/how-to-use-useeffect-hook-in-react-functional-component\/#Create_React_App_For_Using_useEffect_Hook\" >Create React App For Using useEffect Hook<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/programmingfields.com\/how-to-use-useeffect-hook-in-react-functional-component\/#React_useState_Hook\" >React useState Hook<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/programmingfields.com\/how-to-use-useeffect-hook-in-react-functional-component\/#React_useEffect_Hook\" >React useEffect Hook<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/programmingfields.com\/how-to-use-useeffect-hook-in-react-functional-component\/#useEffect_Hook_in_React_js_with_Timeout\" >useEffect Hook in React js with Timeout<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/programmingfields.com\/how-to-use-useeffect-hook-in-react-functional-component\/#React_useEffect_with_Some_Action\" >React useEffect with Some Action<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/programmingfields.com\/how-to-use-useeffect-hook-in-react-functional-component\/#Final_Words\" >Final Words<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prerequisites\"><\/span><strong>Prerequisites<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before moving to this post, I am assuming you are familiar with the below concepts.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>React Basics<\/strong><\/li>\n\n\n\n<li><strong>Class and function component in React<\/strong><\/li>\n\n\n\n<li><strong>State in class and function component<\/strong><\/li>\n\n\n\n<li><strong>useState hook<\/strong><\/li>\n<\/ul>\n\n\n\n<p>For creating a React app, you must have the Node js installed in your system. When you are ready, let&#8217;s hit the below command to create a React app.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_useEffect_hook_in_React_js\"><\/span><strong>What is useEffect hook in React js<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The useEffect hook is a JavaScript function that is used in the functional component in React. It is equivalent to the <strong>React life cycle methods<\/strong> such are &#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>componentDidMount<\/strong><\/li>\n\n\n\n<li><strong>componentDidUpdate<\/strong><\/li>\n\n\n\n<li><strong>componentWillUnmount<\/strong><\/li>\n<\/ul>\n\n\n\n<p>These life cycle methods are used in the class component. So, you cannot go with these methods in the functional component. Hence, React hook has the <strong>useEffect() <\/strong>function to achieve the life cycle methods functionality. It works to perform any action based on certain changes that happened in the component. For example- In the class component the <strong>componentDidMount<\/strong> calls when the component invokes and mounts the first time. <\/p>\n\n\n\n<p>Also, the componentDidUpdate calls when some change happens in the component. Lastly, for the clean up when the component will unmount then the <strong>componentWillUnmount<\/strong> method invokes.<\/p>\n\n\n\n<p>Similarly, in the case of useEffect hook, there are two types of effects-<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Effects Without Cleanup<\/li>\n\n\n\n<li>Effects With Cleanup<\/li>\n<\/ol>\n\n\n\n<p>We will see both types with the example here. So, let&#8217;s start by creating a new app in React.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/how-to-create-loading-spinner-using-bootstrap-in-react-js\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Create Loading Spinner Using Bootstrap in React JS<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_React_App_For_Using_useEffect_Hook\"><\/span><strong>Create React App For Using useEffect Hook<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Open the terminal and hit the below command for creating a new app in React. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">npx create-react-app blog<\/code><\/pre>\n\n\n\n<p>It will take a couple of minutes to install the React library in a new folder named blog.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"803\" height=\"606\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-17-08-41-27.png?resize=803%2C606&#038;ssl=1\" alt=\"Create React App\" class=\"wp-image-6415\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-17-08-41-27.png?w=803&amp;ssl=1 803w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-17-08-41-27.png?resize=300%2C226&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-17-08-41-27.png?resize=768%2C580&amp;ssl=1 768w\" sizes=\"auto, (max-width: 803px) 100vw, 803px\" \/><figcaption class=\"wp-element-caption\"><strong>Create React App<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p>After finishing the installation, let&#8217;s navigate to the app folder and run it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">cd blog\nnpm start<\/code><\/pre>\n\n\n\n<p>It will start the development server on the 3000 port by default. Also, it will redirect to the default browser automatically.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"703\" height=\"413\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-18-20-31-57.png?resize=703%2C413&#038;ssl=1\" alt=\"React App Home Screen\" class=\"wp-image-6420\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-18-20-31-57.png?w=703&amp;ssl=1 703w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/06\/Screenshot-from-2021-06-18-20-31-57.png?resize=300%2C176&amp;ssl=1 300w\" sizes=\"auto, (max-width: 703px) 100vw, 703px\" \/><figcaption class=\"wp-element-caption\"><strong>React Home Screen<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/file-upload-in-react-js-using-laravel-8-restful-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">File Upload in React JS Using Laravel 8 RESTful API<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"React_useState_Hook\"><\/span><strong>React useState Hook<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Initially, we will convert the class component to the function component. So, by default, we have a component named App.js. Therefore, we will be using the same component by removing all the code from it. Now, let&#8217;s convert it to the functional component.<\/p>\n\n\n\n<pre title=\"App.jsx\" class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">import React, { useState } from 'react';\nimport '.\/App.css';\n\nexport const App = () =&gt; {\n  const [message, setMessage] = useState('Hello! this is state hook');\n\n  return (\n    &lt;div className='container'&gt;\n      &lt;h4&gt; {message} &lt;\/h4&gt;\n    &lt;\/div&gt;\n  );\n};\n<\/code><\/pre>\n\n\n\n<p>In the above code, I have created a state and set a message in the initial state. Also, I have imported a stylesheet file named App.css. So, just add the below basic style in it.<\/p>\n\n\n\n<pre title=\"App.css\" class=\"wp-block-code\"><code lang=\"css\" class=\"language-css\">.container {\n  width: 50%;\n  margin: 100px auto;\n  text-align: center;\n}<\/code><\/pre>\n\n\n\n<p>Check the result in the browser.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"643\" height=\"302\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-08-11-10.png?resize=643%2C302&#038;ssl=1\" alt=\"State Hook in React\" class=\"wp-image-6919\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-08-11-10.png?w=643&amp;ssl=1 643w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-08-11-10.png?resize=300%2C141&amp;ssl=1 300w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><figcaption class=\"wp-element-caption\"><strong>State Hook in React<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p>In the above result, we have the message that is coming from the state. Now, let&#8217;s apply the effect on it.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/how-to-use-axios-in-react-js-for-http-requests-handling\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Use Axios in React JS For API Requests Handling<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"React_useEffect_Hook\"><\/span><strong>React useEffect Hook<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For a better understanding of useEffect, I have used the useState hook in the above snippet. Now, let&#8217;s use the effect. For using the effect hook, you will need to import the hook as we did for the state hook.<\/p>\n\n\n\n<p>The basic syntax of the useEffect hook is &#8211;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\"> useEffect(() =&gt; {\n    effect\n    return () =&gt; {\n      cleanup\n    };\n  }, [input]);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the syntax, you may see inside the useEffect function there is the effect section. That means when the component has any change then that effect (action) should apply here.<\/li>\n\n\n\n<li>There is a cleanup that is equivalent to the <strong><meta http-equiv=\"content-type\" content=\"text\/html; charset=utf-8\"><strong>componentWillUnmount<\/strong><\/strong> method. <\/li>\n\n\n\n<li>In the callback there is an array. If you set this array to empty then it will call the effect at the first time when the component will mount. But, if you set some variable or any other action then the effect will be called only when that certain value or action will be there. By default, if you don&#8217;t set any parameter in the callback then it will everytime on render. This may reduce the performance of the application. <\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s understand through an example here. After importing the useEffect from React library, just add the below snippet.<\/p>\n\n\n\n<pre title=\"App.jsx\" class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">import React, { useState, useEffect } from 'react';\nimport '.\/App.css';\n\nexport const App = () =&gt; {\n  const [message, setMessage] = useState('Hello! this is state hook');\n\n  useEffect(() =&gt; {\n    console.log(message);\n    setMessage('This is effect hook');\n  });\n\n  return (\n    &lt;div className='container'&gt;\n      &lt;h4&gt; {message} &lt;\/h4&gt;\n    &lt;\/div&gt;\n  );\n};<\/code><\/pre>\n\n\n\n<p>In the above code, I have set the message inside the effect hook. This will replace the existing message with a new one. See the result the initial state message is replaced when the component is loaded. But, in the console, you can see the message is called two times.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"896\" height=\"298\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-08-44-03.png?resize=896%2C298&#038;ssl=1\" alt=\"useEffect Hook Response\" class=\"wp-image-6928\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-08-44-03.png?w=896&amp;ssl=1 896w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-08-44-03.png?resize=300%2C100&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-08-44-03.png?resize=768%2C255&amp;ssl=1 768w\" sizes=\"auto, (max-width: 896px) 100vw, 896px\" \/><figcaption class=\"wp-element-caption\"><strong>useEffect Hook Response<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p>Also, in the console, you may see the message. The first message is of the initial state. But, when the component is mounted then using its effect it changed the message.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/form-handling-in-react-js-with-validation-using-react-state\/\" target=\"_blank\" rel=\"noreferrer noopener\">Form Handling in React JS With Validation Using React State<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"useEffect_Hook_in_React_js_with_Timeout\"><\/span><strong>useEffect Hook in React js with Timeout<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let&#8217;s see the useEffect with setTimeout() function. Here, we will update the state message with some delay.<\/p>\n\n\n\n<pre title=\"App.jsx\" class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">import React, { useState, useEffect } from 'react';\nimport '.\/App.css';\n\nexport const App = () =&gt; {\n  const [message, setMessage] = useState('Hello! this is state hook');\n  useEffect(() =&gt; {\n    console.log(message);\n    setTimeout(() =&gt; {\n      setMessage('This is effect hook');\n    }, 2000);\n  });\n\n  return (\n    &lt;div className='container'&gt;\n      &lt;h4&gt; {message} &lt;\/h4&gt;\n    &lt;\/div&gt;\n  );\n};<\/code><\/pre>\n\n\n\n<p>Check the response, here, we have updated the message with a delay of 2 seconds.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/CPT2109150903-363x225-1.gif?resize=398%2C241&#038;ssl=1\" alt=\"effect with delay\" class=\"wp-image-6933\" width=\"398\" height=\"241\"\/><figcaption class=\"wp-element-caption\"><strong>effect with delay<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p>If you want to call the effect only one time, not on the basis of each rendering of any other effect. Then you can set the empty array in the callback of the useEffect() function. This will call the effect method only when the component will mount.<\/p>\n\n\n\n<pre title=\"App.jsx\" class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">import React, { useState, useEffect } from 'react';\nimport '.\/App.css';\n\nexport const App = () =&gt; {\n  const [message, setMessage] = useState('Hello! this is state hook');\n  useEffect(() =&gt; {\n    console.log(message);\n    setMessage('This is effect hook');\n  }, []);\n\n  return (\n    &lt;div className='container'&gt;\n      &lt;h4&gt; {message} &lt;\/h4&gt;\n    &lt;\/div&gt;\n  );\n};<\/code><\/pre>\n\n\n\n<p>See the response in the result. The message is changed in the UI and in the console, the message is called once on component mounted.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"806\" height=\"283\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-09-06-38.png?resize=806%2C283&#038;ssl=1\" alt=\"Component Mount Effect in useEffect hook\" class=\"wp-image-6935\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-09-06-38.png?w=806&amp;ssl=1 806w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-09-06-38.png?resize=300%2C105&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-09-06-38.png?resize=768%2C270&amp;ssl=1 768w\" sizes=\"auto, (max-width: 806px) 100vw, 806px\" \/><figcaption class=\"wp-element-caption\"><strong>Component Mount Effect<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/create-react-routing-in-react-js-using-react-router-dom\/\" target=\"_blank\" rel=\"noreferrer noopener\">Brief Overview of React Routing Using React Router DOM<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"React_useEffect_with_Some_Action\"><\/span><strong>React useEffect with Some Action<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let&#8217;s modify the example with a condition and action. We want to call effect when a certain condition will match and also, it required some value to perform the action. <\/p>\n\n\n\n<pre title=\"App.jsx\" class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">import React, { useState, useEffect } from 'react';\nimport '.\/App.css';\n\nexport const App = () =&gt; {\n  const [message, setMessage] = useState('Hello! this is state hook');\n  const [counter, setCounter] = useState(0);\n  \n  useEffect(() =&gt; {\n    console.log(message);\n\n    if (counter === 4) {\n      setMessage('This is effect hook');\n    }\n  }, []);\n  \n\n  const increment = () =&gt; {\n    setCounter(counter+1);    \n  };\n\n  return (\n    &lt;div className='container'&gt;\n      &lt;h4&gt; {message} &lt;\/h4&gt;\n      &lt;h4&gt; {counter} &lt;\/h4&gt;\n\n      &lt;button onClick={increment}&gt; Increment &lt;\/button&gt;\n    &lt;\/div&gt;\n  );\n};\n<\/code><\/pre>\n\n\n\n<p>In the above code, I have added a counter button to increment the counter value by 1. In the useEffect, I have set a condition when the counter value will be 4 the message will be changed. But in the callback, I have not passed any action. It is just an empty array. So, it will be rendered only one time on the component mount.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"868\" height=\"325\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-09-34-52.png?resize=868%2C325&#038;ssl=1\" alt=\"React useEffect with condition\" class=\"wp-image-6938\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-09-34-52.png?w=868&amp;ssl=1 868w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-09-34-52.png?resize=300%2C112&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-09-34-52.png?resize=768%2C288&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-09-34-52.png?resize=270%2C100&amp;ssl=1 270w\" sizes=\"auto, (max-width: 868px) 100vw, 868px\" \/><figcaption class=\"wp-element-caption\"><strong>useEffect with Condition<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p>So, here the message is not changed because the counter is incrementing but in the callback, there is no action. So, the message is not updated.<\/p>\n\n\n\n<p>Now, let&#8217;s pass action in the callback. Here, we want when the counter exists and it is equal to 4. Then it will update the message.<\/p>\n\n\n\n<pre title=\"App.jsx\" class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">import React, { useState, useEffect } from 'react';\nimport '.\/App.css';\n\nexport const App = () =&gt; {\n  const [message, setMessage] = useState('Hello! this is state hook');\n  const [counter, setCounter] = useState(0);\n  \n  useEffect(() =&gt; {\n    console.log(message);\n\n    if (counter === 4) {\n      setMessage('This is effect hook');\n    }\n  }, [counter]);\n  \n\n  const increment = () =&gt; {\n    setCounter(counter+1);    \n  };\n\n  return (\n    &lt;div className='container'&gt;\n      &lt;h4&gt; {message} &lt;\/h4&gt;\n      &lt;h4&gt; {counter} &lt;\/h4&gt;\n\n      &lt;button onClick={increment}&gt; Increment &lt;\/button&gt;\n    &lt;\/div&gt;\n  );\n};<\/code><\/pre>\n\n\n\n<p>The result is here. You can see the effect has been applied on the basis of the counter value and condition.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"859\" height=\"338\" src=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-09-26-44.png?resize=859%2C338&#038;ssl=1\" alt=\"React useEffect and useState hook\" class=\"wp-image-6937\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-09-26-44.png?w=859&amp;ssl=1 859w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-09-26-44.png?resize=300%2C118&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/Screenshot-from-2021-09-15-09-26-44.png?resize=768%2C302&amp;ssl=1 768w\" sizes=\"auto, (max-width: 859px) 100vw, 859px\" \/><figcaption class=\"wp-element-caption\"><strong>useEffect Hook with Action and Condition<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/programmingfields.com\/create-a-todo-app-in-react-using-laravel-8-restful-apis\/\" target=\"_blank\" rel=\"noreferrer noopener\">Create a Todo App in React Using Laravel 8 RESTful APIs<\/a><\/p>\n\n\n\n<p>So, that&#8217;s it for the useEffect hook in React js.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Final_Words\"><\/span><strong>Final Words<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We used the <strong>useState() hook <\/strong>to implement the <strong>useEffect()<\/strong> hook in this post. The useEffect is the equivalent to the React life cycle methods. So, you can perform any action based on the component rendering. As per the life cycle methods, there are different methods that are used in the React class component. But, you cannot use directly the life cycle methods in the functional component. So, you will require to go through the React useEffect Hook in the functional component. I hope this post will be helpful in learning to you at a beginner level.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The React hooks are a very powerful feature that allows us to hook into the React functionality. It is a JavaScript function that can be used or called inside the React functional component. You cannot use a hook inside the custom function in the component. So, it is necessary to call the hook function at [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":6941,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1369],"tags":[2183,2185,2186,2187,2182,2184],"yst_prominent_words":[1370,1401],"class_list":{"0":"post-6903","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-react","8":"tag-effect-hook-in-react","9":"tag-react-hook-in-functional-component","10":"tag-react-life-cycle-methods","11":"tag-react-life-cycle-using-hook","12":"tag-react-useeffect-hook","13":"tag-react-usestate-hook","14":"entry"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2021\/09\/useEffect-Hook-in-React.png?fit=2240%2C1260&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/6903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/comments?post=6903"}],"version-history":[{"count":3,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/6903\/revisions"}],"predecessor-version":[{"id":8981,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/6903\/revisions\/8981"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media\/6941"}],"wp:attachment":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media?parent=6903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/categories?post=6903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/tags?post=6903"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/yst_prominent_words?post=6903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}