DEV Community

Cover image for Run useEffect Only Once :React

Run useEffect Only Once :React

Harish Sankaramanchi on February 13, 2020

If you want to run an effect and clean it up only once (on mount and unmount), you can pass an empty array ([]) as a second argument. This tells R...
Collapse
 
lexlohr profile image
Alex Lohr

Also if you only want to run it on unmount, you can use

useEffect(() => onUnmountHandler, [])
Enter fullscreen mode Exit fullscreen mode

onUnmountHandler will then be called after the component unmounts.