# import {useEffect} from "react";

<div class="WaaZC" id="bkmrk-the-statement-import"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CAwQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIDBAB"><span data-huuid="7831568508111237803">The statement `import {useEffect} from "react";` </span><span data-huuid="7831568508111238862">is a JavaScript import declaration used in React applications. </span></div></div></div><div class="rPeykc" data-hveid="CAwQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIDBAB" id="bkmrk-">  
</div><div class="rPeykc" data-hveid="CAwQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIDBAB" id="bkmrk--1"></div><div class="WaaZC" id="bkmrk-purpose%3A"><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CAoQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIChAB" role="heading">**<span data-huuid="7831568508111240980">Purpose: </span>**</div></div></div><div class="WaaZC" id="bkmrk-this-line-imports-th"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CA4QAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIDhAB"><span data-huuid="7831568508111239002">This line imports the `useEffect` Hook from the React library. </span><span data-huuid="7831568508111240061">The `useEffect` Hook is a fundamental part of functional components in React, enabling developers to perform "side effects." </span></div></div></div><div class="rPeykc" data-hveid="CA4QAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIDhAB" id="bkmrk--2">  
</div><div class="rPeykc" data-hveid="CA4QAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIDhAB" id="bkmrk--3"></div><div class="WaaZC" id="bkmrk-side-effects-in-reac"><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CAgQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQICBAB" role="heading">**<span data-huuid="7831568508111238083">Side Effects in React: </span>**</div></div></div><div class="WaaZC" id="bkmrk-side-effects-are-ope"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc uP58nb MNX06c" data-hveid="CAQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIBBAB"><span data-huuid="7831568508111240201">Side effects are operations that interact with the outside world or have an impact beyond the component's render cycle. </span><span data-huuid="7831568508111237164">Common examples of side effects include: </span></div></div></div><div class="WaaZC" id="bkmrk-data-fetching%3A-makin"><div class="RJPOee EIJn2" style="animation: none !important;">- <span data-huuid="7831568508111240341">**Data fetching:** </span><span data-huuid="7831568508111237304">Making API calls to retrieve data from a server. </span>
- <span data-huuid="7831568508111239422">**DOM manipulation:** </span><span data-huuid="7831568508111240481">Directly interacting with the browser's Document Object Model (e.g., setting the document title). </span>
- <span data-huuid="7831568508111238503">**Subscriptions:** </span><span data-huuid="7831568508111239562">Setting up event listeners or connecting to external services like WebSockets. </span>
- <span data-huuid="7831568508111237584">**Timers:** </span><span data-huuid="7831568508111238643">Using `setTimeout` or `setInterval`. </span>

</div></div><div class="WaaZC" id="bkmrk-how-useeffect-works%3A"><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CCQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJBAB" role="heading"></div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CCQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJBAB" role="heading"></div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CCQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJBAB" role="heading"></div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CCQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJBAB" role="heading">**<span data-huuid="11612000788613407035">How `useEffect` Works: </span>**</div></div></div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CCQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJBAB" id="bkmrk--4" role="heading">  
</div><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CCQQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJBAB" id="bkmrk--5" role="heading"></div><div class="WaaZC" id="bkmrk-useeffect-takes-two-"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc uP58nb" data-hveid="CCYQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQIJhAB">**<span data-huuid="11612000788613410213"><span aria-level="2" role="heading">`useEffect` takes two arguments:</span> </span>**</div></div></div><div class="WaaZC" id="bkmrk-a-setup-function%3A-th"><div class="RJPOee EIJn2" style="animation: none !important;">- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="11612000788613408836">**A setup function:** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="11612000788613408377">This function contains the code for your side effect. </span><span data-huuid="11612000788613407918">It runs after every render of the component (including the initial render) unless its dependencies prevent it from doing so. </span></div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="11612000788613407000">**A dependency array (optional):** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="11612000788613410637">This array specifies the values that the effect depends on. </span><span data-huuid="11612000788613410178">If any of these values change between renders, the effect will re-run. </span><span data-huuid="11612000788613409719">An empty dependency array `[]` ensures the effect runs only once after the initial render (similar to `componentDidMount` in class components). </span></div></div></div></div>

</div></div><div class="WaaZC" id="bkmrk-example-usage%3A"><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CDYQAQ" data-ved="2ahUKEwi6gcHknrCRAxXTUGwGHTezAGgQo_EKegQINhAB" role="heading"><span data-huuid="15663895085194306514">Example Usage: </span></div></div></div><div class="WaaZC" id="bkmrk--6"><div class="RJPOee EIJn2" style="animation: none !important;"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc"><div class="zYSUYd"><div class="FS7GEb"><div class="dDrxod"><div aria-live="polite" class="WDoJJe">  
</div></div></div></div></div></div></div></div>```
import { useEffect, useState } from "react";

function MyComponent() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    // This side effect updates the document title
    document.title = `Count: ${count}`;
  }, [count]); // The effect re-runs whenever 'count' changes

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

export default MyComponent;
```