Skip to content
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

Fix Accessing element.ref was removed in React 19 #74334

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/next/src/client/app-dir/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ function formatStringOrUrl(urlObjOrString: UrlObject | string): string {
return formatUrl(urlObjOrString)
}

/**
* Returns the ref of a React element handling differences between React 19 and older versions.
* It will throw runtime error if the element is not a valid React element.
* @param element React.ReactElement
* @returns React.Ref<any> | undefined
*/
function getReactElementRef(
element: React.ReactElement
): React.Ref<any> | undefined {
// 'ref' is passed as prop in React 19, whereas 'ref' is directly attached to children in older versions
if (parseInt(React.version, 10) >= 19) {
Copy link
Contributor Author

@oliviertassinari oliviertassinari Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure how you guys want to check this, another option is:

Suggested change
if (parseInt(React.version, 10) >= 19) {
if ((element.props as any).propertyIsEnumerable('ref')) {

return (element.props as any).ref
}
// @ts-expect-error element.ref is not included in the ReactElement type
// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/70189
return element.ref
}

/**
* A React component that extends the HTML `<a>` element to provide [prefetching](https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating#2-prefetching)
* and client-side navigation between routes.
Expand Down Expand Up @@ -440,7 +458,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
}

const childRef: any = legacyBehavior
? child && typeof child === 'object' && child.ref
? getReactElementRef(child)
: forwardedRef

const [setIntersectionRef, isVisible, resetVisible] = useIntersection({
Expand Down
20 changes: 19 additions & 1 deletion packages/next/src/client/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@ function formatStringOrUrl(urlObjOrString: UrlObject | string): string {
return formatUrl(urlObjOrString)
}

/**
* Returns the ref of a React element handling differences between React 19 and older versions.
* It will throw runtime error if the element is not a valid React element.
* @param element React.ReactElement
* @returns React.Ref<any> | undefined
*/
function getReactElementRef(
element: React.ReactElement
): React.Ref<any> | undefined {
// 'ref' is passed as prop in React 19, whereas 'ref' is directly attached to children in older versions
if (parseInt(React.version, 10) >= 19) {
return (element.props as any).ref
}
// @ts-expect-error element.ref is not included in the ReactElement type
// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/70189
return element.ref
}

/**
* A React component that extends the HTML `<a>` element to provide [prefetching](https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating#2-prefetching)
* and client-side navigation between routes.
Expand Down Expand Up @@ -458,7 +476,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
}

const childRef: any = legacyBehavior
? child && typeof child === 'object' && child.ref
? getReactElementRef(child)
: forwardedRef

const [setIntersectionRef, isVisible, resetVisible] = useIntersection({
Expand Down
Loading