DEV Community

Discussion on: Common mistake done while using react hooks

 
theirongiant profile image
theirongiant • Edited

'state' in your example will be the same between renders (as long as it hasn't been changed), useState initialises state to the array in the first render but after that will return the same array until it is changed.

If you look at the console output in my codesandbox example it will log 'test' on the intial render but if you click on the 'Change other' button it will trigger a render but you will see that the useEffect doesn't re-run as useState will return the same initial array.

edit

codesandbox.io/s/react-codesandbox...
If you watch the console as you click the button you'll see the child component re-renders without triggering useEffect, useState ensures that the reference to state is stable between renders.