re: Common mistake done while using react hooks VIEW POST

re: The updateArray function creates a new array (with a different reference) which will get recognised by React and will trigger the useEffect. Corr...

'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.

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.

code of conduct - report abuse