{"id":129,"date":"2024-04-02T17:13:41","date_gmt":"2024-04-02T17:13:41","guid":{"rendered":"https:\/\/devdojo.co.in\/?p=129"},"modified":"2024-04-02T17:13:42","modified_gmt":"2024-04-02T17:13:42","slug":"useeffect-the-most-powerful-react-hook","status":"publish","type":"post","link":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/","title":{"rendered":"useEffect(): The Most Powerful React Hook"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\ud83d\ude80 Dive into ReactJS&#8217;s dynamic world with the versatile useEffect hook! \ud83c\udfa3<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The old ways of using traditional class components are over; now, useEffect reigns supreme, bringing the robust lifecycle functionalities to functional components in ReactJS!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In React, managing side effects is crucial for building robust and efficient applications. <code>useEffect<\/code> is a React Hook that allows developers to perform side effects in function components. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding how to use <code>useEffect<\/code> effectively is essential for writing clean and maintainable code. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The useEffect hook stands out as one of the most extensively utilized features in ReactJS. It empowers developers to mimic the behavior of lifecycle methods found in React Class Components within Functional components.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we&#8217;ll explore the <code>useEffect<\/code> Hook in-depth, covering its syntax, use cases, common pitfalls, and best practices with illustrative examples and scenarios. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">What is useEffect?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>useEffect<\/code> is a built-in React Hook that enables developers to perform side effects in function components. Side effects may include data fetching, subscriptions, or manually changing the DOM in React components.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It serves the same purpose as lifecycle methods (such as <code>componentDidMount<\/code>, <code>componentDidUpdate<\/code>, and <code>componentWillUnmount<\/code>) in class components.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u2728 What does useEffect offer?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Seamlessly replicate mounting, updating, and unmounting operations.<br>Simplify code structure with its two indispensable parameters:<br><br>1\ufe0f\u20e3 Callback function<br>2\ufe0f\u20e3 Optional dependency array<br><br>Let&#8217;s explore the four distinct variants of useEffect:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Execute Once, Similar to componentDidMount:<\/strong> When an empty dependency array is passed as the second argument, the effect is triggered only once after rendering.<\/li>\n\n\n\n<li><strong>Run on Every Render (Comparable to the Render Method):<\/strong>The effect runs every time the component renders.<\/li>\n\n\n\n<li><strong>Execute on Every Update(Similar to componentWillUpdate or shouldComponentUpdate):<\/strong> The effect is invoked on every update to the component.<\/li>\n\n\n\n<li><strong>Cleanup Operation (Comparable to componentDidUnMount):<\/strong> Use useEffect for cleanup tasks after the component unmounts.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Syntax:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The basic syntax of <code>useEffect<\/code> is as follows:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-Monaspace-Neon.woff\" style=\"font-size:clamp(12px, .75rem, 18px);font-family:Code-Pro-Monaspace-Neon,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2d333d;color:#9eadbd\">JavaScript<\/span><span role=\"button\" tabindex=\"0\" data-code=\"import React, { useEffect } from 'react';\n\nfunction MyComponent() {\n  useEffect(() =&gt; {\n    \/\/ Side effect code goes here\n    return () =&gt; {\n      \/\/ Cleanup code (optional) goes here\n    };\n  }, [dependencies]);\n  \n  \/\/ Component JSX\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> React, { useEffect } <\/span><span style=\"color: #F47067\">from<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&#39;react&#39;<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">function<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">MyComponent<\/span><span style=\"color: #ADBAC7\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #DCBDFB\">useEffect<\/span><span style=\"color: #ADBAC7\">(() <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #768390\">\/\/ Side effect code goes here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> () <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      <\/span><span style=\"color: #768390\">\/\/ Cleanup code (optional) goes here<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    };<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }, [dependencies]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #768390\">\/\/ Component JSX<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first argument to <code>useEffect<\/code> is a function that represents the side effect.<\/li>\n\n\n\n<li>The second argument is an optional array of dependencies. If provided, the effect will only re-run if any of the dependencies have changed. If omitted, the effect runs after every render.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The second argument is an optional array of dependencies, so we may leave it black array or we can completely remove it.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Scenarios:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>@ No Dependency Array:<\/strong> If we don&#8217;t provide the dependency array as second argument, then the effect will run on every render.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-Monaspace-Neon.woff\" style=\"font-size:clamp(12px, .75rem, 18px);font-family:Code-Pro-Monaspace-Neon,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2d333d;color:#9eadbd\">JavaScript<\/span><span role=\"button\" tabindex=\"0\" data-code=\"import React, { useEffect } from 'react';\n\nfunction MyComponent() {\n  useEffect(() =&gt; {\n     \/\/ run on every render.. \n  });  \/\/ &lt;-- no dependency array passed.\n  \n  \/\/ Component JSX\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> React, { useEffect } <\/span><span style=\"color: #F47067\">from<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&#39;react&#39;<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">function<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">MyComponent<\/span><span style=\"color: #ADBAC7\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #DCBDFB\">useEffect<\/span><span style=\"color: #ADBAC7\">(() <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">     <\/span><span style=\"color: #768390\">\/\/ run on every render.. <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  });  <\/span><span style=\"color: #768390\">\/\/ &lt;-- no dependency array passed.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #768390\">\/\/ Component JSX<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>@ Blank Dependency Array:<\/strong> If we pass an empty dependency array as second argument; then it\u2019ll be called once when the component will be mounted. This will work like class component\u2019s componentDidMount() lifecycle method.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-Monaspace-Neon.woff\" style=\"font-size:clamp(12px, .75rem, 18px);font-family:Code-Pro-Monaspace-Neon,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2d333d;color:#9eadbd\">JavaScript<\/span><span role=\"button\" tabindex=\"0\" data-code=\"import React, { useEffect } from 'react';\n\nfunction MyComponent() {\n  useEffect(() =&gt; {\n     \/\/ Runs only on the first render\n  }, []);  \/\/ &lt;-- empty dependency array passed.\n  \n  \/\/ Component JSX\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> React, { useEffect } <\/span><span style=\"color: #F47067\">from<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&#39;react&#39;<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">function<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">MyComponent<\/span><span style=\"color: #ADBAC7\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #DCBDFB\">useEffect<\/span><span style=\"color: #ADBAC7\">(() <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">     <\/span><span style=\"color: #768390\">\/\/ Runs only on the first render<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }, []);  <\/span><span style=\"color: #768390\">\/\/ &lt;-- empty dependency array passed.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #768390\">\/\/ Component JSX<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>@ Pass the Dependency:<\/strong> if we provide one or many dependencies in dependency array, then this function will run on every time when any dependency value will change or updated. This will mimic the same functionality of componentDidUpdate or shouldComponentUpdate lifecycle method.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-Monaspace-Neon.woff\" style=\"font-size:clamp(12px, .75rem, 18px);font-family:Code-Pro-Monaspace-Neon,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2d333d;color:#9eadbd\">JavaScript<\/span><span role=\"button\" tabindex=\"0\" data-code=\"import React, { useEffect } from 'react';\n\nfunction MyComponent() {\n  useEffect(() =&gt; {\n     \/\/ Runs on every time when dependency value will update.\n  }, [dep1, dep2,...,depN]);  \/\/ &lt;-- array of dependencies\n  \n  \/\/ Component JSX\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> React, { useEffect } <\/span><span style=\"color: #F47067\">from<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&#39;react&#39;<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">function<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">MyComponent<\/span><span style=\"color: #ADBAC7\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #DCBDFB\">useEffect<\/span><span style=\"color: #ADBAC7\">(() <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">     <\/span><span style=\"color: #768390\">\/\/ Runs on every time when dependency value will update.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }, [dep1, dep2,<\/span><span style=\"color: #F47067\">...<\/span><span style=\"color: #ADBAC7\">,depN]);  <\/span><span style=\"color: #768390\">\/\/ &lt;-- array of dependencies<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #768390\">\/\/ Component JSX<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>@ Cleanup Operation:<\/strong> When we pass the return statement with a function (cleanup function) inside the useEffect; then it will perform cleanup or unmounting. React will call this cleanup function each time before the Effect runs again, and one final time when the component unmount (get removed). This Effect will mimic the componentWillUnmount lifecycle method of class components.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-Monaspace-Neon.woff\" style=\"font-size:clamp(12px, .75rem, 18px);font-family:Code-Pro-Monaspace-Neon,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2d333d;color:#9eadbd\">JavaScript<\/span><span role=\"button\" tabindex=\"0\" data-code=\"import React, { useEffect } from 'react';\n\nfunction MyComponent() {\n  useEffect(() =&gt; {\n     someEventListner();\n     \n     reutn() =&gt; {\n       removeSomeEventListner() {\n         \/\/ do something for cleanup\n       }\n     }\n  }, []);  \/\/ &lt;-- array of dependencies\n  \n  \/\/ Component JSX\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> React, { useEffect } <\/span><span style=\"color: #F47067\">from<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&#39;react&#39;<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">function<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">MyComponent<\/span><span style=\"color: #ADBAC7\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #DCBDFB\">useEffect<\/span><span style=\"color: #ADBAC7\">(() <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">     <\/span><span style=\"color: #DCBDFB\">someEventListner<\/span><span style=\"color: #ADBAC7\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">     <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">     <\/span><span style=\"color: #DCBDFB\">reutn<\/span><span style=\"color: #ADBAC7\">() <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">       <\/span><span style=\"color: #DCBDFB\">removeSomeEventListner<\/span><span style=\"color: #ADBAC7\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">         <\/span><span style=\"color: #768390\">\/\/ do something for cleanup<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">       }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">     }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }, []);  <\/span><span style=\"color: #768390\">\/\/ &lt;-- array of dependencies<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #768390\">\/\/ Component JSX<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Examples:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong># Example 1: Only run the effect once on render<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-Monaspace-Neon.woff\" style=\"font-size:clamp(12px, .75rem, 18px);font-family:Code-Pro-Monaspace-Neon,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2d333d;color:#9eadbd\">JavaScript<\/span><span role=\"button\" tabindex=\"0\" data-code=\"import { useState, useEffect } from &quot;react&quot;;\nimport ReactDOM from &quot;react-dom\/client&quot;;\n\nfunction Timer() {\n  const [count, setCount] = useState(0);\n\n  useEffect(() =&gt; {\n    setTimeout(() =&gt; {\n      setCount((count) =&gt; count + 1);\n    }, 1000);\n  }, []); \/\/ &lt;- empty dependency array...\n\n  return &lt;h1&gt;I've rendered {count} times!&lt;\/h1&gt;;\n}\n\nconst root = ReactDOM.createRoot(document.getElementById('root'));\nroot.render(&lt;Timer \/&gt;);\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> { useState, useEffect } <\/span><span style=\"color: #F47067\">from<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;react&quot;<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> ReactDOM <\/span><span style=\"color: #F47067\">from<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;react-dom\/client&quot;<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">function<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">Timer<\/span><span style=\"color: #ADBAC7\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">const<\/span><span style=\"color: #ADBAC7\"> [<\/span><span style=\"color: #6CB6FF\">count<\/span><span style=\"color: #ADBAC7\">, <\/span><span style=\"color: #6CB6FF\">setCount<\/span><span style=\"color: #ADBAC7\">] <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">useState<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #6CB6FF\">0<\/span><span style=\"color: #ADBAC7\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #DCBDFB\">useEffect<\/span><span style=\"color: #ADBAC7\">(() <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #DCBDFB\">setTimeout<\/span><span style=\"color: #ADBAC7\">(() <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      <\/span><span style=\"color: #DCBDFB\">setCount<\/span><span style=\"color: #ADBAC7\">((<\/span><span style=\"color: #F69D50\">count<\/span><span style=\"color: #ADBAC7\">) <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> count <\/span><span style=\"color: #F47067\">+<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">1<\/span><span style=\"color: #ADBAC7\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    }, <\/span><span style=\"color: #6CB6FF\">1000<\/span><span style=\"color: #ADBAC7\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }, []); <\/span><span style=\"color: #768390\">\/\/ &lt;- empty dependency array...<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> &lt;<\/span><span style=\"color: #8DDB8C\">h1<\/span><span style=\"color: #ADBAC7\">&gt;I&#39;ve rendered <\/span><span style=\"color: #F47067\">{<\/span><span style=\"color: #ADBAC7\">count<\/span><span style=\"color: #F47067\">}<\/span><span style=\"color: #ADBAC7\"> times!&lt;\/<\/span><span style=\"color: #8DDB8C\">h1<\/span><span style=\"color: #ADBAC7\">&gt;;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">const<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">root<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> ReactDOM.<\/span><span style=\"color: #DCBDFB\">createRoot<\/span><span style=\"color: #ADBAC7\">(document.<\/span><span style=\"color: #DCBDFB\">getElementById<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&#39;root&#39;<\/span><span style=\"color: #ADBAC7\">));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">root.<\/span><span style=\"color: #DCBDFB\">render<\/span><span style=\"color: #ADBAC7\">(&lt;<\/span><span style=\"color: #8DDB8C\">Timer<\/span><span style=\"color: #ADBAC7\"> \/&gt;);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Here we added the empty dependency array <code>[]<\/code> as second argument in <code>useEffect<\/code>, this function will run only once when component is mounted or rendered.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong># Example 2: Run every time when dependency will update<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-Monaspace-Neon.woff\" style=\"font-size:clamp(12px, .75rem, 18px);font-family:Code-Pro-Monaspace-Neon,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2d333d;color:#9eadbd\">JavaScript<\/span><span role=\"button\" tabindex=\"0\" data-code=\"import { useState, useEffect } from &quot;react&quot;;\nimport ReactDOM from &quot;react-dom\/client&quot;;\n\nfunction Counter() {\n  const [count, setCount] = useState(0);\n  const [calculation, setCalculation] = useState(0);\n\n  useEffect(() =&gt; {\n    setCalculation(() =&gt; count * 2);\n  }, [count]); \/\/ &lt;- count as dependency\n\n  return (\n    &lt;&gt;\n      &lt;p&gt;Count: {count}&lt;\/p&gt;\n      &lt;button onClick={() =&gt; setCount((c) =&gt; c + 1)}&gt;+&lt;\/button&gt;\n      &lt;p&gt;Calculation: {calculation}&lt;\/p&gt;\n    &lt;\/&gt;\n  );\n}\n\nconst root = ReactDOM.createRoot(document.getElementById('root'));\nroot.render(&lt;Counter \/&gt;);\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> { useState, useEffect } <\/span><span style=\"color: #F47067\">from<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;react&quot;<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> ReactDOM <\/span><span style=\"color: #F47067\">from<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&quot;react-dom\/client&quot;<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">function<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">Counter<\/span><span style=\"color: #ADBAC7\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">const<\/span><span style=\"color: #ADBAC7\"> [<\/span><span style=\"color: #6CB6FF\">count<\/span><span style=\"color: #ADBAC7\">, <\/span><span style=\"color: #6CB6FF\">setCount<\/span><span style=\"color: #ADBAC7\">] <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">useState<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #6CB6FF\">0<\/span><span style=\"color: #ADBAC7\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">const<\/span><span style=\"color: #ADBAC7\"> [<\/span><span style=\"color: #6CB6FF\">calculation<\/span><span style=\"color: #ADBAC7\">, <\/span><span style=\"color: #6CB6FF\">setCalculation<\/span><span style=\"color: #ADBAC7\">] <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">useState<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #6CB6FF\">0<\/span><span style=\"color: #ADBAC7\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #DCBDFB\">useEffect<\/span><span style=\"color: #ADBAC7\">(() <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #DCBDFB\">setCalculation<\/span><span style=\"color: #ADBAC7\">(() <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> count <\/span><span style=\"color: #F47067\">*<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">2<\/span><span style=\"color: #ADBAC7\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }, [count]); <\/span><span style=\"color: #768390\">\/\/ &lt;- count as dependency<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    &lt;&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      &lt;<\/span><span style=\"color: #8DDB8C\">p<\/span><span style=\"color: #ADBAC7\">&gt;Count: <\/span><span style=\"color: #F47067\">{<\/span><span style=\"color: #ADBAC7\">count<\/span><span style=\"color: #F47067\">}<\/span><span style=\"color: #ADBAC7\">&lt;\/<\/span><span style=\"color: #8DDB8C\">p<\/span><span style=\"color: #ADBAC7\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      &lt;<\/span><span style=\"color: #8DDB8C\">button<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">onClick<\/span><span style=\"color: #F47067\">={<\/span><span style=\"color: #ADBAC7\">() <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">setCount<\/span><span style=\"color: #ADBAC7\">((<\/span><span style=\"color: #F69D50\">c<\/span><span style=\"color: #ADBAC7\">) <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> c <\/span><span style=\"color: #F47067\">+<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">1<\/span><span style=\"color: #ADBAC7\">)<\/span><span style=\"color: #F47067\">}<\/span><span style=\"color: #ADBAC7\">&gt;+&lt;\/<\/span><span style=\"color: #8DDB8C\">button<\/span><span style=\"color: #ADBAC7\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      &lt;<\/span><span style=\"color: #8DDB8C\">p<\/span><span style=\"color: #ADBAC7\">&gt;Calculation: <\/span><span style=\"color: #F47067\">{<\/span><span style=\"color: #ADBAC7\">calculation<\/span><span style=\"color: #F47067\">}<\/span><span style=\"color: #ADBAC7\">&lt;\/<\/span><span style=\"color: #8DDB8C\">p<\/span><span style=\"color: #ADBAC7\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    &lt;\/&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  );<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">const<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">root<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> ReactDOM.<\/span><span style=\"color: #DCBDFB\">createRoot<\/span><span style=\"color: #ADBAC7\">(document.<\/span><span style=\"color: #DCBDFB\">getElementById<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&#39;root&#39;<\/span><span style=\"color: #ADBAC7\">));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">root.<\/span><span style=\"color: #DCBDFB\">render<\/span><span style=\"color: #ADBAC7\">(&lt;<\/span><span style=\"color: #8DDB8C\">Counter<\/span><span style=\"color: #ADBAC7\"> \/&gt;);<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Here we pass the <strong>count<\/strong> as dependency. If the count value updates, the effect will run again.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong># Example 3: Cleanup<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Some time we require the cleanup to reduce memory leaks. Subscriptions, event listeners, timeouts, and other effects that are no longer needed should be removed or disposed.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-Monaspace-Neon.woff\" style=\"font-size:clamp(12px, .75rem, 18px);font-family:Code-Pro-Monaspace-Neon,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2d333d;color:#9eadbd\">JavaScript<\/span><span role=\"button\" tabindex=\"0\" data-code=\"import React, { useState, useEffect } from 'react';\nimport io from 'socket.io-client';\n\nfunction RealtimeComponent() {\n  const [messages, setMessages] = useState([]);\n\n  useEffect(() =&gt; {\n    const socket = io('https:\/\/realtime.example.com');\n    socket.on('message', message =&gt; {\n      setMessages(prevMessages =&gt; [...prevMessages, message]);\n    });\n\n    return () =&gt; {\n      socket.disconnect();\n    };\n  }, []);\n\n  return (\n    &lt;div&gt;\n      &lt;ul&gt;\n        {messages.map((message, index) =&gt; (\n          &lt;li key={index}&gt;{message}&lt;\/li&gt;\n        ))}\n      &lt;\/ul&gt;\n    &lt;\/div&gt;\n  );\n}\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> React, { useState, useEffect } <\/span><span style=\"color: #F47067\">from<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&#39;react&#39;<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">import<\/span><span style=\"color: #ADBAC7\"> io <\/span><span style=\"color: #F47067\">from<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #96D0FF\">&#39;socket.io-client&#39;<\/span><span style=\"color: #ADBAC7\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F47067\">function<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">RealtimeComponent<\/span><span style=\"color: #ADBAC7\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">const<\/span><span style=\"color: #ADBAC7\"> [<\/span><span style=\"color: #6CB6FF\">messages<\/span><span style=\"color: #ADBAC7\">, <\/span><span style=\"color: #6CB6FF\">setMessages<\/span><span style=\"color: #ADBAC7\">] <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">useState<\/span><span style=\"color: #ADBAC7\">([]);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #DCBDFB\">useEffect<\/span><span style=\"color: #ADBAC7\">(() <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">const<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">socket<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">=<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #DCBDFB\">io<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&#39;https:\/\/realtime.example.com&#39;<\/span><span style=\"color: #ADBAC7\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    socket.<\/span><span style=\"color: #DCBDFB\">on<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #96D0FF\">&#39;message&#39;<\/span><span style=\"color: #ADBAC7\">, <\/span><span style=\"color: #F69D50\">message<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      <\/span><span style=\"color: #DCBDFB\">setMessages<\/span><span style=\"color: #ADBAC7\">(<\/span><span style=\"color: #F69D50\">prevMessages<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> [<\/span><span style=\"color: #F47067\">...<\/span><span style=\"color: #ADBAC7\">prevMessages, message]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    });<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> () <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      socket.<\/span><span style=\"color: #DCBDFB\">disconnect<\/span><span style=\"color: #ADBAC7\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    };<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  }, []);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #F47067\">return<\/span><span style=\"color: #ADBAC7\"> (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    &lt;<\/span><span style=\"color: #8DDB8C\">div<\/span><span style=\"color: #ADBAC7\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      &lt;<\/span><span style=\"color: #8DDB8C\">ul<\/span><span style=\"color: #ADBAC7\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">        <\/span><span style=\"color: #F47067\">{<\/span><span style=\"color: #ADBAC7\">messages.<\/span><span style=\"color: #DCBDFB\">map<\/span><span style=\"color: #ADBAC7\">((<\/span><span style=\"color: #F69D50\">message<\/span><span style=\"color: #ADBAC7\">, <\/span><span style=\"color: #F69D50\">index<\/span><span style=\"color: #ADBAC7\">) <\/span><span style=\"color: #F47067\">=&gt;<\/span><span style=\"color: #ADBAC7\"> (<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">          &lt;<\/span><span style=\"color: #8DDB8C\">li<\/span><span style=\"color: #ADBAC7\"> <\/span><span style=\"color: #6CB6FF\">key<\/span><span style=\"color: #F47067\">={<\/span><span style=\"color: #ADBAC7\">index<\/span><span style=\"color: #F47067\">}<\/span><span style=\"color: #ADBAC7\">&gt;<\/span><span style=\"color: #F47067\">{<\/span><span style=\"color: #ADBAC7\">message<\/span><span style=\"color: #F47067\">}<\/span><span style=\"color: #ADBAC7\">&lt;\/<\/span><span style=\"color: #8DDB8C\">li<\/span><span style=\"color: #ADBAC7\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">        ))<\/span><span style=\"color: #F47067\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">      &lt;\/<\/span><span style=\"color: #8DDB8C\">ul<\/span><span style=\"color: #ADBAC7\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    &lt;\/<\/span><span style=\"color: #8DDB8C\">div<\/span><span style=\"color: #ADBAC7\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  );<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Here, we establish a WebSocket connection to receive real-time messages. The effect is run once when the component mounts, and we clean up the socket connection when the component unmounts.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Common Pitfalls and Best Practices:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Forgetting Dependency Array<\/strong>: Always specify dependencies in the dependency array to avoid unintended side effects and infinite loops.<\/li>\n\n\n\n<li><strong>Incorrect Cleanup<\/strong>: Ensure to clean up resources (such as event listeners, subscriptions, or timers) in the cleanup function returned by <code>useEffect<\/code>.<\/li>\n\n\n\n<li><strong>Conditional Effects<\/strong>: Be cautious when using conditionals inside <code>useEffect<\/code>, as it can lead to unpredictable behavior. Consider using separate <code>useEffect<\/code> hooks for different conditions.<\/li>\n\n\n\n<li><strong>Performance Optimization<\/strong>: Use the dependency array to optimize performance by specifying only the necessary dependencies.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Conclusion:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>useEffect<\/code> is a powerful tool in React for managing side effects in function components. By understanding its syntax, use cases, and best practices, developers can write cleaner, more maintainable code. Through examples and scenarios, we&#8217;ve explored how to handle subscriptions, and avoid common pitfalls. Mastering <code>useEffect<\/code> is essential for building high-quality React applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Embrace efficiency and flexibility in your ReactJS projects with useEffect! \ud83d\udca1<br><br>I&#8217;d love to hear your thoughts and insights on the topic. Feel free to leave your comments and share with anyone who might find it helpful. Together, let&#8217;s keep learning and growing!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\ude80 Dive into ReactJS&#8217;s dynamic world with the versatile useEffect hook! \ud83c\udfa3 The old ways of using traditional class components are over; now, useEffect reigns supreme, bringing the robust lifecycle functionalities to functional components in ReactJS! In React, managing side effects is crucial for building robust and efficient applications. useEffect is a React Hook that allows developers to perform side effects in function components. Understanding how to use useEffect effectively&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":142,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-reactjs"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.2.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"\ud83d\ude80 Dive into ReactJS&#039;s dynamic world with the versatile useEffect hook! \ud83c\udfa3 The old ways of using traditional class components are over; now, useEffect reigns supreme, bringing the robust lifecycle functionalities to functional components in ReactJS! In React, managing side effects is crucial for building robust and efficient applications. useEffect is a React Hook that\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Lokendra\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"DevDojo - Empowering Developers\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"useEffect(): The Most Powerful React Hook - DevDojo\" \/>\n\t\t<meta property=\"og:description\" content=\"\ud83d\ude80 Dive into ReactJS&#039;s dynamic world with the versatile useEffect hook! \ud83c\udfa3 The old ways of using traditional class components are over; now, useEffect reigns supreme, bringing the robust lifecycle functionalities to functional components in ReactJS! In React, managing side effects is crucial for building robust and efficient applications. useEffect is a React Hook that\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2024-04-02T17:13:41+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2024-04-02T17:13:42+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"useEffect(): The Most Powerful React Hook - DevDojo\" \/>\n\t\t<meta name=\"twitter:description\" content=\"\ud83d\ude80 Dive into ReactJS&#039;s dynamic world with the versatile useEffect hook! \ud83c\udfa3 The old ways of using traditional class components are over; now, useEffect reigns supreme, bringing the robust lifecycle functionalities to functional components in ReactJS! In React, managing side effects is crucial for building robust and efficient applications. useEffect is a React Hook that\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2024\\\/04\\\/02\\\/useeffect-the-most-powerful-react-hook\\\/#blogposting\",\"name\":\"useEffect(): The Most Powerful React Hook - DevDojo\",\"headline\":\"useEffect(): The Most Powerful React Hook\",\"author\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/React-Hook-useEffect.gif\",\"width\":720,\"height\":405},\"datePublished\":\"2024-04-02T17:13:41+00:00\",\"dateModified\":\"2024-04-02T17:13:42+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2024\\\/04\\\/02\\\/useeffect-the-most-powerful-react-hook\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2024\\\/04\\\/02\\\/useeffect-the-most-powerful-react-hook\\\/#webpage\"},\"articleSection\":\"ReactJS\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2024\\\/04\\\/02\\\/useeffect-the-most-powerful-react-hook\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/devdojo.co.in\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/frontend\\\/#listItem\",\"name\":\"Frontend\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/frontend\\\/#listItem\",\"position\":2,\"name\":\"Frontend\",\"item\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/frontend\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/frontend\\\/reactjs\\\/#listItem\",\"name\":\"ReactJS\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/frontend\\\/reactjs\\\/#listItem\",\"position\":3,\"name\":\"ReactJS\",\"item\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/frontend\\\/reactjs\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2024\\\/04\\\/02\\\/useeffect-the-most-powerful-react-hook\\\/#listItem\",\"name\":\"useEffect(): The Most Powerful React Hook\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/frontend\\\/#listItem\",\"name\":\"Frontend\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2024\\\/04\\\/02\\\/useeffect-the-most-powerful-react-hook\\\/#listItem\",\"position\":4,\"name\":\"useEffect(): The Most Powerful React Hook\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/frontend\\\/reactjs\\\/#listItem\",\"name\":\"ReactJS\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\",\"name\":\"DevDojo\",\"description\":\"Empowering Developers\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/\",\"name\":\"Lokendra\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2024\\\/04\\\/02\\\/useeffect-the-most-powerful-react-hook\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/99f102f0cc9080237ce54b91304fd1b3b6fa29925cbaeba93cd796872e58b45c?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Lokendra\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2024\\\/04\\\/02\\\/useeffect-the-most-powerful-react-hook\\\/#webpage\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2024\\\/04\\\/02\\\/useeffect-the-most-powerful-react-hook\\\/\",\"name\":\"useEffect(): The Most Powerful React Hook - DevDojo\",\"description\":\"\\ud83d\\ude80 Dive into ReactJS's dynamic world with the versatile useEffect hook! \\ud83c\\udfa3 The old ways of using traditional class components are over; now, useEffect reigns supreme, bringing the robust lifecycle functionalities to functional components in ReactJS! In React, managing side effects is crucial for building robust and efficient applications. useEffect is a React Hook that\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2024\\\/04\\\/02\\\/useeffect-the-most-powerful-react-hook\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/React-Hook-useEffect.gif\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2024\\\/04\\\/02\\\/useeffect-the-most-powerful-react-hook\\\/#mainImage\",\"width\":720,\"height\":405},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2024\\\/04\\\/02\\\/useeffect-the-most-powerful-react-hook\\\/#mainImage\"},\"datePublished\":\"2024-04-02T17:13:41+00:00\",\"dateModified\":\"2024-04-02T17:13:42+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#website\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/\",\"name\":\"DevDojo\",\"description\":\"Empowering Developers\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"useEffect(): The Most Powerful React Hook - DevDojo","description":"\ud83d\ude80 Dive into ReactJS's dynamic world with the versatile useEffect hook! \ud83c\udfa3 The old ways of using traditional class components are over; now, useEffect reigns supreme, bringing the robust lifecycle functionalities to functional components in ReactJS! In React, managing side effects is crucial for building robust and efficient applications. useEffect is a React Hook that","canonical_url":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/#blogposting","name":"useEffect(): The Most Powerful React Hook - DevDojo","headline":"useEffect(): The Most Powerful React Hook","author":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"publisher":{"@id":"https:\/\/devdojo.co.in\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/devdojo.co.in\/wp-content\/uploads\/2024\/04\/React-Hook-useEffect.gif","width":720,"height":405},"datePublished":"2024-04-02T17:13:41+00:00","dateModified":"2024-04-02T17:13:42+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/#webpage"},"isPartOf":{"@id":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/#webpage"},"articleSection":"ReactJS"},{"@type":"BreadcrumbList","@id":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/#listItem","position":1,"name":"Home","item":"https:\/\/devdojo.co.in\/","nextItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/frontend\/#listItem","name":"Frontend"}},{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/frontend\/#listItem","position":2,"name":"Frontend","item":"https:\/\/devdojo.co.in\/index.php\/category\/frontend\/","nextItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/frontend\/reactjs\/#listItem","name":"ReactJS"},"previousItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/frontend\/reactjs\/#listItem","position":3,"name":"ReactJS","item":"https:\/\/devdojo.co.in\/index.php\/category\/frontend\/reactjs\/","nextItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/#listItem","name":"useEffect(): The Most Powerful React Hook"},"previousItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/frontend\/#listItem","name":"Frontend"}},{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/#listItem","position":4,"name":"useEffect(): The Most Powerful React Hook","previousItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/frontend\/reactjs\/#listItem","name":"ReactJS"}}]},{"@type":"Organization","@id":"https:\/\/devdojo.co.in\/#organization","name":"DevDojo","description":"Empowering Developers","url":"https:\/\/devdojo.co.in\/"},{"@type":"Person","@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author","url":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/","name":"Lokendra","image":{"@type":"ImageObject","@id":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/99f102f0cc9080237ce54b91304fd1b3b6fa29925cbaeba93cd796872e58b45c?s=96&d=mm&r=g","width":96,"height":96,"caption":"Lokendra"}},{"@type":"WebPage","@id":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/#webpage","url":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/","name":"useEffect(): The Most Powerful React Hook - DevDojo","description":"\ud83d\ude80 Dive into ReactJS's dynamic world with the versatile useEffect hook! \ud83c\udfa3 The old ways of using traditional class components are over; now, useEffect reigns supreme, bringing the robust lifecycle functionalities to functional components in ReactJS! In React, managing side effects is crucial for building robust and efficient applications. useEffect is a React Hook that","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/devdojo.co.in\/#website"},"breadcrumb":{"@id":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/#breadcrumblist"},"author":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"creator":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"image":{"@type":"ImageObject","url":"https:\/\/devdojo.co.in\/wp-content\/uploads\/2024\/04\/React-Hook-useEffect.gif","@id":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/#mainImage","width":720,"height":405},"primaryImageOfPage":{"@id":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/#mainImage"},"datePublished":"2024-04-02T17:13:41+00:00","dateModified":"2024-04-02T17:13:42+00:00"},{"@type":"WebSite","@id":"https:\/\/devdojo.co.in\/#website","url":"https:\/\/devdojo.co.in\/","name":"DevDojo","description":"Empowering Developers","inLanguage":"en-US","publisher":{"@id":"https:\/\/devdojo.co.in\/#organization"}}]},"og:locale":"en_US","og:site_name":"DevDojo - Empowering Developers","og:type":"article","og:title":"useEffect(): The Most Powerful React Hook - DevDojo","og:description":"\ud83d\ude80 Dive into ReactJS's dynamic world with the versatile useEffect hook! \ud83c\udfa3 The old ways of using traditional class components are over; now, useEffect reigns supreme, bringing the robust lifecycle functionalities to functional components in ReactJS! In React, managing side effects is crucial for building robust and efficient applications. useEffect is a React Hook that","og:url":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/","article:published_time":"2024-04-02T17:13:41+00:00","article:modified_time":"2024-04-02T17:13:42+00:00","twitter:card":"summary_large_image","twitter:title":"useEffect(): The Most Powerful React Hook - DevDojo","twitter:description":"\ud83d\ude80 Dive into ReactJS's dynamic world with the versatile useEffect hook! \ud83c\udfa3 The old ways of using traditional class components are over; now, useEffect reigns supreme, bringing the robust lifecycle functionalities to functional components in ReactJS! In React, managing side effects is crucial for building robust and efficient applications. useEffect is a React Hook that"},"aioseo_meta_data":{"post_id":"129","title":null,"description":null,"keywords":[],"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2024-04-02 15:45:49","updated":"2025-08-18 13:19:39","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/devdojo.co.in\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/devdojo.co.in\/index.php\/category\/frontend\/\" title=\"Frontend\">Frontend<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/devdojo.co.in\/index.php\/category\/frontend\/reactjs\/\" title=\"ReactJS\">ReactJS<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tuseEffect(): The Most Powerful React Hook\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/devdojo.co.in\/"},{"label":"Frontend","link":"https:\/\/devdojo.co.in\/index.php\/category\/frontend\/"},{"label":"ReactJS","link":"https:\/\/devdojo.co.in\/index.php\/category\/frontend\/reactjs\/"},{"label":"useEffect(): The Most Powerful React Hook","link":"https:\/\/devdojo.co.in\/index.php\/2024\/04\/02\/useeffect-the-most-powerful-react-hook\/"}],"_links":{"self":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":12,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":141,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/129\/revisions\/141"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/media\/142"}],"wp:attachment":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}