Proposes fundamental changes to React's dependency comparison mechanism which would require deep framework knowledge.
The issue reports infinite rendering when objects/arrays are used in useEffect dependencies due to reference comparison. It suggests either adding deep comparison functionality or better error detection. This would require core React changes with significant architectural implications and potential performance tradeoffs.
React is not preventing infinite rendering/looping and results in freezing of browser.
The root cause of this issue is because the dependency array of useEffect compares object/array by reference. But, this is a serious issue because
useEffect does not have a single reference to this gotcha - guarding against passing object/array to the dependency array, since reference of the object/array is being checked, not a deep comparison value. Some solutions already exist like https://github.com/kentcdodds/use-deep-compare-effect, https://stackoverflow.com/questions/54095994/react-useeffect-comparing-objects/63965274#63965274. But, only people who are aware of this behaviour of useEffect will reach out to these solutions (or they might manually deep compare with their own custom hook as some people do in the linked stackoverflow link)It would also be helpful if useEffect includes a helpful option like
useEffect(() => {
// myArray, myObject gets new reference on evey render
}, [myArray, myObject], {
deepCompare: true/false // it would be helpful to have this instead of reaching for custom hooks/npm packages
})
React version:
16.12.x (any latest version)
codesandbox link - https://codesandbox.io/s/apollo-client-uselazyquery-example-forkeClaim this issue to let others know you're working on it. You'll earn 10 points when you complete it!