reactjs / reactjs/react.dev

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

Open
#1,730 8 comments 16 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.