How to get refs from another component in React JS

前端 未结 4 577
时光取名叫无心
时光取名叫无心 2021-02-12 15:44

The code in main App component is as follows :

class App extends Component {
  componentDidMount(){
      console.log(this.ref);
      debugger;
  }

  render()         


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-12 16:24

    ref is property of each this.props.children hence you can access ref of child component in parent via ref property on this.props.children

    Make sure you access ref after componentDidMount

    Edit :

    Try below set of code if this works :

    var myChild= React.Children.only(this.props.children);
    var clone = React.cloneElement(myChild, { ref: "myRef" });
    

提交回复
热议问题