It's an easy way of explaining and understanding how useEffect works. But I agree. Maybe it would be better to create an example using something like window dimensions which from my point of view requires the useEffect hook. Needed for adding and removing the event listener.
The clean-up function runs before the component is removed from the UI to prevent memory leaks. Additionally, if a component renders multiple times (as they typically do), the previous effect is cleaned up before executing the next effect.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
It's an easy way of explaining and understanding how
useEffect
works. But I agree. Maybe it would be better to create an example using something like window dimensions which from my point of view requires theuseEffect
hook. Needed for adding and removing the event listener.useWindowDimensions custom hook
From React docs
The clean-up function runs before the component is removed from the UI to prevent memory leaks. Additionally, if a component renders multiple times (as they typically do), the previous effect is cleaned up before executing the next effect.