<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ajay</title>
    <description>The latest articles on DEV Community by Ajay (@ajay_056).</description>
    <link>https://dev.to/ajay_056</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4081182%2Fc778b7c1-0adf-4262-8d7e-d020a948199f.jpg</url>
      <title>DEV Community: Ajay</title>
      <link>https://dev.to/ajay_056</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ajay_056"/>
    <language>en</language>
    <item>
      <title>UseEffect-Hook</title>
      <dc:creator>Ajay</dc:creator>
      <pubDate>Thu, 01 Oct 2026 09:40:57 +0000</pubDate>
      <link>https://dev.to/ajay_056/useeffect-hook-267n</link>
      <guid>https://dev.to/ajay_056/useeffect-hook-267n</guid>
      <description>&lt;h2&gt;
  
  
  1. Run Only Once
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Print "Component Mounted" only once when page loads.
&lt;/h3&gt;

&lt;h4&gt;
  
  
  -How will you write useEffect?
&lt;/h4&gt;

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

&lt;h4&gt;
  
  
  -What should be in dependency array? Why?
&lt;/h4&gt;

&lt;p&gt;The dependency array should like  an empty array "[ ]" because the empty array run the useEffect when the components  is mounted.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Run on Every Render
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Print "Rendered" on every render.
&lt;/h3&gt;

&lt;h4&gt;
  
  
  - How will you write useEffect?
&lt;/h4&gt;

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

&lt;h4&gt;
  
  
  - What happens if dependency array is not provided?
&lt;/h4&gt;

&lt;p&gt;If the dependency array is not provided, useEffect runs after every render of the component. Therefore, when a state change causes the component to re-render, the useEffect runs again.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Run When State Changes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  const [count, setCount] = useState(0);
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Log "Count changed" only when count changes.
&lt;/h3&gt;

&lt;h4&gt;
  
  
  - What should dependency array contain?
&lt;/h4&gt;

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

&lt;p&gt;The dependency array should contain the state variables that the useEffect should watch for changes.&lt;/p&gt;

&lt;h4&gt;
  
  
  - What happens if it is empty [ ]?
&lt;/h4&gt;

&lt;p&gt;If we give an empty [ ] ,The useEffect will run only once when the  first time component is mounted.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Two States, One Effect
&lt;/h2&gt;

&lt;h3&gt;
  
  
  const [count, setCount] = useState(0);
&lt;/h3&gt;

&lt;h3&gt;
  
  
  const [name, setName] = useState("");
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Run effect when count OR name changes.
&lt;/h3&gt;

&lt;h4&gt;
  
  
  - How will dependency array look?
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fog7tmkzfxs0pvf69oias.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fog7tmkzfxs0pvf69oias.png" alt=" " width="624" height="456"&gt;&lt;/a&gt;&lt;br&gt;
The dependancy array look like an both count and name in the single array "[count , name] ".&lt;/p&gt;

&lt;h4&gt;
  
  
  - What if only count is added?
&lt;/h4&gt;

&lt;p&gt;If only count is added to the dependency array, the useEffect watches only for changes in count. When count changes, the useEffect runs again. If name changes, this useEffect does not run because name is not included in the dependency array.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Derived Logic
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Show "Even Number" whenever count becomes even.
&lt;/h3&gt;

&lt;h3&gt;
  
  
  - Should this logic be inside render or useEffect? Why?
&lt;/h3&gt;

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

&lt;p&gt;The logic is inside useEffect because we want to perform the console log when count changes. The [count] dependency makes the UseEffect to run whenever count changes, and the if condition checks whether the new count is even.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Infinite Loop Trap
&lt;/h2&gt;

&lt;h3&gt;
  
  
  useEffect(() =&amp;gt; {
&lt;/h3&gt;

&lt;h3&gt;
  
  
  setCount(count + 1);
&lt;/h3&gt;

&lt;h3&gt;
  
  
  }, [count]);
&lt;/h3&gt;

&lt;h4&gt;
  
  
  - What will happen?
&lt;/h4&gt;

&lt;p&gt;In this the dependancy array has[count],The UseEffect will runs whenever the count variable is changed because the useEffect itself incrementing the change  in state(setCount(count+1) ,Therefore the continous loop has created.&lt;/p&gt;

&lt;h4&gt;
  
  
  - Why does this cause infinite re-render?
&lt;/h4&gt;

&lt;p&gt;The useEffect runs whenever count changes because count is in the dependency array. Inside the useEffect, setCount(count + 1) changes the count state. This causes the component to re-render. Since count changed, the useEffect runs again and changes count again. This process repeats continuously, causing an infinite loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Fix the Infinite Loop
&lt;/h2&gt;

&lt;h4&gt;
  
  
  - How can you increment count only once using useEffect?
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fop43oe463avxef38b9zd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fop43oe463avxef38b9zd.png" alt=" " width="250" height="109"&gt;&lt;/a&gt;&lt;br&gt;
If we give an empty dependency array "[ ]" means it runs only once and inside the useEffect the setCount(count +1) changes the state therefore  the useEffect run and the initial  value is increment only once.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Document Title Sync
&lt;/h2&gt;

&lt;h3&gt;
  
  
  useEffect(() =&amp;gt; {
&lt;/h3&gt;

&lt;h3&gt;
  
  
  document.title = count;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  }, [?]);
&lt;/h3&gt;

&lt;h4&gt;
  
  
  - What should go inside ? Why?
&lt;/h4&gt;

&lt;p&gt;The dependency array should contain [count] because the useEffect runs every state of change in count and inside the useEffect the document.title were mounted to  count there the result of the count is displayed in the  document.title .&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn8352n2aq3wansrnwwap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fn8352n2aq3wansrnwwap.png" alt=" " width="594" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Cleanup Basic
&lt;/h2&gt;

&lt;h3&gt;
  
  
  useEffect(() =&amp;gt; {
&lt;/h3&gt;

&lt;h3&gt;
  
  
  window.addEventListener("click", handleClick);
&lt;/h3&gt;

&lt;h3&gt;
  
  
  });
&lt;/h3&gt;

&lt;h4&gt;
  
  
  - What problem may happen?
&lt;/h4&gt;

&lt;p&gt;The event listener is added after every render, so handleClick may be called multiple times for a single click.&lt;/p&gt;

&lt;h4&gt;
  
  
  - How to fix properly?
&lt;/h4&gt;

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

&lt;h4&gt;
  
  
  - When does cleanup run?
&lt;/h4&gt;

&lt;p&gt;Cleanup runs when the component is unmounted.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. API Call on Mount
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Fetch users when component loads.
&lt;/h3&gt;

&lt;h4&gt;
  
  
  - Why do we use [ ]?
&lt;/h4&gt;

&lt;p&gt;We use an empty dependency array [ ] because we want the API call to run only once after the component's initial render.&lt;/p&gt;

&lt;h4&gt;
  
  
  - What happens if we remove [ ]?
&lt;/h4&gt;

&lt;p&gt;If we remove [ ], the useEffect runs after every render. Therefore, the API call will be made after every render, which can cause unnecessary API requests and may lead to performance problems.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>reactjsdevelopment</category>
    </item>
  </channel>
</rss>
