Aamir
Aamir

Reputation: 355

How to access ref of Second child from first child within same Parent in ReactJS

I have Parent component containing two child component.

<Parent>
<Child1></Child1>
<Child2></Child2>
</Parent>

In Child2 component I have one Ul (unorder list), in which I have given ref="mylist".

<ul ref="mylist" className="list"><li>Test1</li><li>Test2</li><li>Test3</li></ul>

So how can I access mylist from Child1 component and get list values.

Upvotes: 0

Views: 606

Answers (2)

Sid
Sid

Reputation: 189

use React.createRef() inside parent and pass that as prop to children.

Upvotes: 0

Dima Vishnyakov
Dima Vishnyakov

Reputation: 1542

You may consider to store values inside Parent component and pass them as props to children.

Upvotes: 1

Related Questions