I want to make my child components have a ref, but I don't want the user to have to specify the ref when they create the component. So say I have something like this:
<Parent>
<Child />
<Child />
</Parent>
And I want the parent to be able to access the Child components' state. The easy way is to add a ref to each component, but I would like this to the be something that is done during the constructor() function to abstract this away from the end user, as I'd like these components to be generalized.
Is there a clean way to make it so the parent can access the state of the child components, such as when the Child is created you have something like:
class Child extends Component {
constructor(){
super();
this.state = {'abc': 123}
this.ref=Math.random();
}
}
So that inside of the Parent class I can do something like:
class Parent extends Component {
componentWillMount(){
console.log(this.refs);
}
}
So I want to be able to declare a set of components like:
class App extends Component {
render(){
<Parent> <Child /> <Child /> </Parent>
}
}
So that I can access each child and it's state as the parent iterates through child components.
I needed this exact same thing for a form I was making and this is what I came up with to add the refs to the child components automatically
Given that you have:
<Parent>
<Child />
<Child />
</Parent>
You can use this in the Parent component render function to add a ref to every child.
render () {
let wrappedChildren = [];
React.Children.map(this.props.children, (child, i)=> {
if (!child) {
return;
}
let refName = 'child' + i
wrappedChildren.push(React.cloneElement(child, {
key: refName,
ref: refName,
}
));
}, this);
return (
<View>
{wrappedChildren}
</View>
)
}
This will add a ref to every Child consisting of a string plus the index of the current map and then render them in the same order, giving you the equivalent of doing this:
<Parent>
<Child ref="child0" />
<Child ref="child1" />
</Parent>
I know that string refs are deprecated but i was trying to keep it simple you can also use refs as callbacks by changing:
ref: refName
to:
ref: (c) => { this[refName] = c }
And then used it in the parent component as this.child0 for example.
Hope it helps.
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