0

In below code:

<MyComponent>
  <Link to="/">Home</Link>
</MyComponent>

and then , because of the < Link /> ,There will be create a tag < a >

how can I get the Ref of < a >?

Thanks !

eusrh
  • 71
  • 1
  • 6

2 Answers2

0

Simply add the ref attribute.

<MyComponent>
  <Link to="/" ref="homeLink">Home</Link>
</MyComponent>

If you are attempting to apply something to the a element node itself (which I assume you are), you can:

var homeLink = ReactDOM.findDOMNode(this.refs.homeLink);

Then you can add classes, change styles, etc. to the homeLink variable as it references the actual DOM node and not the React Link component. Just make sure to do this after MyComponent renders (e.g. the componentDidMount() method in the lifecycle).

Smaft
  • 142
  • 10
0

I needed to find this to lock focus in a modal nav bar.

findDomNode works, at least for now, but it may be on track for deprecation. It turns out that Link has a native innerRef attribute that you can use in the exact same way you would use ref on a normal element. Check it out here.

wassona
  • 319
  • 1
  • 9