Best practices for useEffect and custom hooks.
When to use useEffect:
When NOT to use useEffect:
useMemo)useQuery instead)Custom hooks:
Only use useEffect for external systems outside our frontend codebase:
Critical rule: Only include dependencies if you want the effect to re-run when they change.
Use useRef for values that shouldn't trigger re-runs but need to stay current:
Always return cleanup functions for subscriptions, timers, and event listeners:
Always extract React logic into custom hooks. Keep components display-only.
🚨 ALL HOOKS MUST BE UNIT TESTED 🚨
See Unit Testing for comprehensive hook testing patterns with React Testing Library.
For more detailed technical guidance, see these reference files: