Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get ref of props.children?

Tags:

reactjs

I want to get ref of Child component. What is the best way to do this?

class Child extends React.Component {
  render() {
    return <div>Child</div>;
  }
}   

class GetRef extends React.Component {

  componentDidMount() {
     console.log(this.props.children.ref)
  }


  render() {
    return this.props.children
  }
}

Edit: So I can use it like this

<GetRef><Child/></GetRef>
like image 636
ais Avatar asked Sep 06 '18 17:09

ais


People also ask

Can we pass ref as props?

Regular function or class components don't receive the ref argument, and ref is not available in props either. Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too.

Can props be passed from child to parent?

To pass data from a child component to its parent, we can call a parent function from the child component with arguments. The parent function can be passed down to the child as a prop, and the function arguments are the data that the parent will receive.

How do I pass all props to my child?

If you don't know what the props are or don't want to type them all out, then instead use the ... spread operator to pass all props simultaneously. Each of the child components above receives all of the props passed into ParentComponent from its parents (in this case, from an App component).

What is props children in react?

“ React.Children provides utilities for dealing with the props.children opaque data structure” — React documentation Why is props.children an “opaque data structure”? Because props.children can consist of one, many, or no child elements, which means that its value can be a single child node, an array of child nodes, or undefined respectively.

Why mastering props children is important?

Mastering props.children is essential to becoming a great React developer and beginning to harness the full potential of the React component model. props.children is one of React’s most useful features, since it gives us the ability to render child components. Because of this, every developer should know how to use it properly.

How to deal with props without taking all possible types?

Thanks to the React.Children API we can easily deal with props.children without taking each of its possible types into account. Thankfully, everything will be handled for us in the background. At the time of writing, React.Children offers five different utilities:

What are props children in JSX?

Essentially, props.children is a special prop, automatically passed to every component, that can be used to render the content included between the opening and closing tags when invoking a component. These kinds of components are identified by the official documentation as “boxes”. Identifying components by JSX syntax


1 Answers

I assumed that GetRef children has only one child, then you can retrieve the ref of the only child component with this code

class Child extends React.Component {

  render() {
    return <div>Child</div>;
  }
}  

class GetRef extends React.Component {

  componentDidMount() {
    console.log(this.ref);
  }

  render() {
    const childElement = React.Children.only(this.props.children);

    return React.cloneElement(
      childElement, 
      { ref: el => this.ref = el }
    );
  }

}

class App extends Component {

  render() {
    return <GetRef><Child/></GetRef>;
  }

}

Here is the complete example on stackblitz

If this.props.children has more than one child, you will need to iterate over the children and store all the refs into an array

like image 169
Olivier Boissé Avatar answered Oct 19 '22 12:10

Olivier Boissé