Q5: What is the useEffect Hook and how is its dependency array used?
The useEffect hook enables functional components to perform side effects (like data fetching, subscriptions, or DOM mutations). Its behavior is controlled by a dependency array: omitting it runs every render, an empty array [] runs once on mount, and specifying dependencies [dep] runs when those values change.
Key Takeaways
useEffectencapsulates side-effect operations in functional components.The dependency array dictates when the effect executes (mount, update, or unmount cleanup).
Knowledge Check
When does a useEffect callback run if passed an empty dependency array []?