Skip to content

Commit

Permalink
Fix Accessing element.ref was removed in React 19
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Dec 27, 2024
1 parent 1ea3ba0 commit a089535
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
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) {
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

0 comments on commit a089535

Please sign in to comment.