DEV Community

Cover image for React useEffect and objects as dependency - 4 approaches to avoid unnecessary executions

React useEffect and objects as dependency - 4 approaches to avoid unnecessary executions

Johannes Kettmann on March 22, 2024

React’s useEffect hook can lead to tricky situations. If you’re not careful it can cause unnecessary executions of the effect or even infinite re-r...
Collapse
 
brense profile image
Rense Bakker

Option 1 should be to cleanup the dirty state with the built-in useMemo hook:

const filters = useMemo(() => ({ brand: "Nike", color: "red" }), [])
Enter fullscreen mode Exit fullscreen mode

Problem solved 😁