Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

jquense
Copy link
Member

@jquense jquense commented Jun 28, 2025

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

@jquense jquense requested a review from kyletsang June 28, 2025 13:44
const refASetter = toFnRef(refA)
const refBSetter = toFnRef(refB)

if (isReact19) {
Copy link
Collaborator

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?

Copy link
Member Author

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

Copy link
Collaborator

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

Copy link
Member Author

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

Copy link
Collaborator

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);
      };
    }
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants