DEV Community

Discussion on: React Hooks Dependencies and Stale Closures

Collapse
 
payapula profile image
payapula

Thanks for the comments 🤝

Yes a ref could be used to remove mentioning it in deps array, but if you use some variable inside a hook and tell to react that it doesn’t depend on it (even if it’s true) then probably in the future, when some other dev try to refactor the code they may run into the stale closure problem which is not very easy to find.

I think It’s not a bad idea to put that in a dependency array even if we know that it won’t change. If it changes and we are not using the fresh value inside the hook then we probably want to simplify our hook.

Dan has written an excellent article covering the exact use case of lying to react about dependencies. - Don’t Lie to react

Collapse
 
lexlohr profile image
Alex Lohr

The use case here is if you only depend on the value of the variable at the time the effect runs.

This can actually be a performance benefit, as you do not render more often than necessary.

Otherwise, you should be aware that you are creating a potential tripwire.