useEffect is the escape hatch for side effects. It allows your React components to synchronize with external systems, from browser APIs to network servers, in a predictable way.
1The Synchronization Philosophy
Unlike lifecycle methods in old React (componentDidMount), useEffect is about synchronization. It asks: 'Given the current state and props, what should my component be doing with the outside world?' If the inputs change, React re-syncs by running the effect again. This ensures your component and the external system are always in lock-step.
2The Cleanup Cycle
Side effects often create resources that need to be removed (timers, subscriptions, event listeners). The cleanup function is the 'garbage collector' of your component. It runs before the effect is re-run and when the component is finally destroyed. Mastering cleanup is the difference between a high-performance app and one that suffers from memory leaks.
