'use client'
import Link from 'next/link'
import * as React from 'react'
import {
useCallback,
useState,
type RefCallback,
type Ref,
type ComponentPropsWithRef,
type ReactNode,
} from 'react'
export default function Page() {
return (
<>
Home
Go to /link-target
>
)
}
function ToggleVisibility({ children }: { children: ReactNode }) {
const [isVisible, setIsVisible] = useState(true)
return (
<>
{isVisible ? children : null}
>
)
}
function AnchorThatDoesRefMerging({
ref,
children,
...anchorProps
}: ComponentPropsWithRef<'a'>) {
const customRef: RefCallback = useCallback((el) => {
if (el) {
console.log('hello friends i am here')
} else {
console.log('goodbye friends i am gone')
}
}, [])
const finalRef = useBuggyRefMerge(customRef, ref ?? null)
return (
{children}
)
}
/** A ref-merging function that doesn't account for cleanup refs (added in React 19)
* https://react.dev/blog/2024/12/05/react-19#cleanup-functions-for-refs
*/
function useBuggyRefMerge(
refA: Ref,
refB: Ref
): RefCallback {
return useCallback(
(current: TElement | null) => {
for (const ref of [refA, refB]) {
if (!ref) {
continue
}
if (typeof ref === 'object') {
ref.current = current
} else {
// BUG!!!
// This would work in 18, but in 19 it can return a cleanup which will get swallowed here
ref(current)
}
}
},
[refA, refB]
)
}