reading-notes

Component Lifecycle / useEffect Hook

useEffect hook

  1. Main intended use case for the useEffect hook: The useEffect hook is primarily used for performing side effects in functional components. Side effects are anything that interacts with the system outside of the function being executed, such as data fetching, setting up a subscription, or manually changing the DOM. useEffect is used to handle these operations that occur outside the normal flow of rendering.

  2. How the effect’s logic interacts with the component: The function passed to useEffect (the “effect”) runs after every render, including the initial one. However, you can control when the effect runs by specifying dependencies in the second argument to useEffect. If you pass an empty array [], the effect will only run once after the initial render, similar to componentDidMount in class components. If you pass an array of dependencies, the effect will run whenever any of those dependencies change.

  3. Importance of the return value from the effect’s logic function: The function you pass to useEffect can optionally return a cleanup function. This cleanup function runs before the component is removed from the UI to prevent memory leaks, as well as before re-running the effect due to subsequent renders. It’s similar to componentWillUnmount in class components. This is important for cleaning up any side effects that should not persist (like clearing timeouts, intervals, or canceling API calls), and helps maintain performance and prevent bugs.