
14/05/2025
â
Main Uses of useEffect ---
1. đĻ Fetching Data
Run API calls when the component mounts or when a dependency changes.
Example: Load user data from a server.
2. đĩī¸ââī¸ Listening to Events
Add event listeners (like scroll, resize), and remove them when no longer needed.
3. âŗ Setting Timers
Use setTimeout or setInterval safely, and clear them to avoid memory leaks.
4. đ§ŧ Cleanup on Unmount
Clean up subscriptions or listeners when the component unmounts.
Example: Disconnect WebSocket or stop an animation.
5. đ Reacting to State/Prop Changes
Trigger logic when a specific state or props change.
Example: Run animation when isVisible becomes true.