← React.js learning path
React.js · Module 7 of 10

Effects with useEffect

Run side effects like data fetching, timers and subscriptions after render.

📖 Key ideas

  1. useEffect(() => { ... }, [deps]) runs after render.
  2. Empty array [] runs once after the first render.
  3. Include every value the effect uses in the dependency array.
  4. Return a cleanup function to remove listeners or timers.
  5. Fetch data in an effect and store it in state.
💡 Real-life example: useEffect is like setting an alarm after you arrive somewhere, and turning it off (cleanup) when you leave.
useEffectHook for side effects
Dependency arrayValues that re-run the effect when changed
CleanupFunction returned to undo the effect
Side effectWork outside rendering, e.g. fetch