Depending on the push function of useRouter causes an infinite loop when the push function is called from inside a useEffect
[[...slug]].jsconst BugExample = () => {
useEffect(() => {
push('/[[...slug]]', '/123/test', { shallow: true })
console.log('called');
},[push])
return null
}
You will see an infinite loop of called in the console
Push should be a non-changing reference, so that the react diff algoirithm knows not to call side effects again uneccesarily
Hi @dan-cooke. I don't believe the various router.* methods use useCallback, therefore between renders push !== push.
Based on a few of the examples from the docs, router or push are not added as dependencies in the dependency array of useEffect.
It seems like a good workaround would be to exclude push from the dependency array.
Hi @jamesmosier - I would be of the opinion that the various router methods _should_ be using useCallback
The React hooks lint rule will complain if you do not include all dependencies in the array - we would prefer to not have to disable this lint rule.
Most helpful comment
Hi @jamesmosier - I would be of the opinion that the various router methods _should_ be using
useCallbackThe React hooks lint rule will complain if you do not include all dependencies in the array - we would prefer to not have to disable this lint rule.