You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Render a Link inside a component and pass a stable callback function as ref (e.g console.log). Feel free to do the same with a native element to see the normal behavior
Trigger a re-render in the parent component, e.g by increasing a counter state
The callback passed to the ref is only called when either the callback changes, or if the underlying node changes.
Actual Behavior
The callback gets called twice every time the Link is re-rendered, once with "null" and then with the actual anchor element. This can cause infinite re-renders in case the ref callback updates a state
This is probably because the refs are merged with a non-memoized mergeRefs function. Commonly that would be wrapped in a a useCallback.
I'm using React Router as a...
library
Reproduction
Link
inside a component and pass a stable callback function asref
(e.gconsole.log
). Feel free to do the same with a native element to see the normal behaviorhttps://codesandbox.io/p/sandbox/react-router-link-ref-repro-forked-dhm6sn
System Info
Used Package Manager
pnpm
Expected Behavior
The callback passed to the
ref
is only called when either the callback changes, or if the underlying node changes.Actual Behavior
The callback gets called twice every time the Link is re-rendered, once with "null" and then with the actual anchor element. This can cause infinite re-renders in case the ref callback updates a state
This is probably because the refs are merged with a non-memoized
mergeRefs
function. Commonly that would be wrapped in a auseCallback
.react-router/packages/react-router/lib/dom/lib.tsx
Lines 644 to 645 in 7b04181
The text was updated successfully, but these errors were encountered: