Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 11.8k
- Forks
- 7.9k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 11
Description
I have the folowing part of code:
private rootRef = React.createRef<HTMLDivElement>();
const target = (
<div>
<Icon design='accepted' size='xl' />
</div>
);
return (
<React.Fragment>
{React.cloneElement(target, {
ref: this.rootRef,
onMouseOver: this.handleMouseOver,
onMouseLeave: this.handleMouseLeave,
})}
{open && this.renderTooltip()}
</React.Fragment>
);
When target with div element (or any other instead of div) everything is ok, but when target is:
const target = (
<Icon design='accepted' size='xl' />
);
I get error: Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Icon is a span element inside of which is svg.
I will be very appreciate for any help.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No repository file, test, or documentation page is named. Start by reviewing the React.cloneElement and ref behavior shown in the reproduction, then identify whether an existing react.dev page needs clarification. Done would require a clearly scoped documentation change and a way to verify the updated guidance.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100