react.dev
react.dev copied to clipboard
[Docs] React.cloneElement and forwarding ref
Hi, I believe the documentation about React.cloneElement is not very clear about handling refs.
I am talking about that part: https://reactjs.org/docs/react-api.html#cloneelement
However, it also preserves refs. This means that if you get a child with a ref on it, you won’t accidentally steal it from your ancestor. You will get the same ref attached to your new element.
Now, let's see consider the following use case (simplified):
const ChangeTitle = ({ children, ...otherProps }) =>
React.cloneElement(children, {
...otherProps,
title: 'Title was changed'
})
function MyComponent() {
const myRef = React.useRef()
return (
<ChangeTitle ref={myRef}>
<span>This is my component</span>
</ChangeTitle>
)
}
This throws a console warning about ChangeTitle unable to hold a ref.
In my original understanding of the docs, I though ChangeTitle would become the cloned element, which, as the docs explains, retain it's original ref, hance the one given by Mycomponent.
Instead, this is obviously wrong, and the cloned element would only retain a ref if it was given to itself, not the parent component, hence something like <span ref={spanRef}>This is my component</span>.
I guess the fix to the above code is the following:
const ChangeTitle = React.forwardRef(({ children, ...otherProps }, ref) =>
React.cloneElement(children, {
...otherProps,
ref,
title: 'Title was changed'
}))
Correct?
Is it worth adding specifically that cloneElement can contain the ref prop if used with React.forwardRef in a functional component?