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>
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.
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.
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).
“ 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.
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.
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:
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With