Ref callbacks. Instead of a ref object you can pass a function: <input ref={(node) => …} />. React calls it during the commit, after the node is in the DOM and before layout effects run. Use it to act when a node appears: store it, observe it, measure it.
On detach (node removed, or callback changed):
- If the callback returned a function (React 19+), React calls that cleanup.
- Otherwise React calls the callback again with
null. The docs say this fallback "will be removed in a future version".
Identity matters. React compares the ref value between renders. An inline arrow is a new function every render, so every commit detaches the old one and attaches the new one, even for the same node. Returning a cleanup doesn't prevent that; it only replaces the null call. A stable function (defined outside the component, or wrapped in useCallback) runs only on mount and unmount.
Refs to lists. Hooks can't run per item, so give each item a callback that stores its node in a Map keyed by id and deletes it in the cleanup.
ref as a prop (React 19). A function component receives ref in its props like any other prop and passes it to the element it wants to expose: function MyInput({ ref }) { return <input ref={ref} />; }. The React 19 post says new components "will no longer need forwardRef" and "In future versions we will deprecate and remove forwardRef." It still works in 19.3.1, without a warning. A component that ignores ref leaves the parent's ref at null.
Class components are unchanged: ref points to the instance and isn't in this.props.
Strict Mode runs one extra setup and cleanup on mount in development. In TypeScript, implicit returns from ref callbacks are now type errors.