Pass ref as a prop in React
Remove forwardRef from a React 19 input wrapper while keeping the DOM node that existing callers expect.
Your search box wraps an input, and a parent component needs to focus it. If the app requires React 19, the wrapper can receive ref with its other props and pass it to the input.
Pass the ref to the element that takes focus
Function components can receive ref as a prop in React 19. Take the old render function out of forwardRef and move its second argument into the props. Inside, keep passing the ref to the same element.
This wrapper owns the label. The parent owns the ref and uses it when you press Focus search.
import { useRef } from 'react'
function SearchField({ label, ref, ...inputProps }) {
return (
<label>
{label}
<input {...inputProps} ref={ref} type="search" />
</label>
)
}
export default function SearchPage() {
const searchRef = useRef(null)
return (
<>
<button type="button" onClick={() => searchRef.current?.focus()}>
Focus search
</button>
<SearchField label="Search the catalog" ref={searchRef} name="query" />
</>
)
}Click Focus search and the input receives keyboard focus. The label still names the search field, and the wrapper still passes name="query" to the input.
Keep the ref's target stable
React assigns the DOM node after mounting. Read current in the click handler, when the field is on the page. The optional chain also handles a missing node if you later make the input conditional.
Keep the ref on the input if existing callers use .focus() or read .value. Moving it to the label might look harmless in a JSX review, but callers would receive a different element. If the old wrapper exposes a custom handle, preserve the handle as part of its interface while you change how the wrapper receives the ref.
Keep forwardRef in a component library release that still supports React 18. When migrating a wrapper in a React 19 app, test it with a caller that uses the ref, such as the focus button above.