-
Notifications
You must be signed in to change notification settings - Fork 27
fix: merged ref behavior in react 19 #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
src/useMergedRefs.ts
Outdated
const refASetter = toFnRef(refA) | ||
const refBSetter = toFnRef(refB) | ||
|
||
if (isReact19) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need a react 19 check? Could it return the cleanup function regardless of version, which would be ignored in R18 and actually work in R19?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know. I think there are react warnings if you return stuff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok - in that case, this code is missing the return handler for anything below R19
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ya but that is the same as now right? no return for < 19
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I meant the mergeRefs
function. mergeRefs
currently doesn't return a function on R18 and below
You meant to do this right?
export function mergeRefs<T>(refA?: Ref<T> | null, refB?: Ref<T> | null) {
const refASetter = toFnRef(refA);
const refBSetter = toFnRef(refB);
return (value: T | null) => {
const cleanupA = refASetter?.(value);
const cleanupB = refBSetter?.(value);
if (isReact19) { // <--- This moved inside
return () => {
cleanUp(cleanupA, refASetter);
cleanUp(cleanupB, refBSetter);
};
}
};
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yes, that is correct. b/c < 19 refs can't return anything
Co-authored-by: Kyle Tsang <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
React 19 supports clean up functions, which we need to handle. This also ensures that the current behavior of not returning a callback still results in them being called/set with null