react.dev icon indicating copy to clipboard operation
react.dev copied to clipboard

add dependency array to examples

Open donavon opened this issue 5 years ago • 4 comments
trafficstars

The usePrevious example is missing a dependency on value.

donavon avatar Nov 23 '19 20:11 donavon

Deploy preview for reactjs ready!

Built with commit d2c26fbd70d59db9c29aebfe054daeecc3a9f613

https://deploy-preview-2584--reactjs.netlify.com

reactjs-bot avatar Nov 23 '19 20:11 reactjs-bot

BTW, This was prompted by this Twitter thread.

https://twitter.com/GabeRicard/status/1198316044957421568

donavon avatar Nov 23 '19 20:11 donavon

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.

rimunroe avatar Nov 24 '19 01:11 rimunroe

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.

RebekahEloise avatar Feb 17 '23 05:02 RebekahEloise