Getting DOM node from React child element

后端 未结 6 1467
余生分开走
余生分开走 2020-12-04 17:14

Using the React.findDOMNode method that was introduced in v0.13.0 I am able to get the DOM node of each child component that was passed into a parent by mapping

6条回答
  •  孤街浪徒
    2020-12-04 18:01

    this.props.children should either be a ReactElement or an array of ReactElement, but not components.

    To get the DOM nodes of the children elements, you need to clone them and assign them a new ref.

    render() {
      return (
        
    {React.Children.map(this.props.children, (element, idx) => { return React.cloneElement(element, { ref: idx }); })}
    ); }

    You can then access the child components via this.refs[childIdx], and retrieve their DOM nodes via ReactDOM.findDOMNode(this.refs[childIdx]).

提交回复
热议问题