react.dev
react.dev copied to clipboard
add dependency array to examples
The usePrevious example is missing a dependency on value.
Deploy preview for reactjs ready!
Built with commit d2c26fbd70d59db9c29aebfe054daeecc3a9f613
https://deploy-preview-2584--reactjs.netlify.com
BTW, This was prompted by this Twitter thread.
https://twitter.com/GabeRicard/status/1198316044957421568
This hook shouldn’t need a dependency array, and I don’t think we want people adding dependency arrays when they don’t need to.
Specifically: there’s no semantic reason to limit the effect to only run when the identity of the argument changes since repeated assignments of the same value have no effect.
The useEffect hook takes 2 arguments, a callback, and a dependency array. Therefore, it seems reasonable to include a dependency array for these two instances of useEffect.