How to pass data between child and parent in react-native?
To call Parent's method from child, you can pass the reference like this.
Parent Class
<ChildClass
onRef={ref => (this.parentReference = ref)}
parentReference = {this.parentMethod.bind(this)}
/>
parentMethod(data) {
}
Child Class
let data = {
id: 'xyz',
name: 'zzz',
};
//This will call the method of parent
this.props.parentReference(data);
You need to pass from parent to child callback function, and then call it in the child.
For example:
class Parent extends React.Component {
constructor(props){
super(props);
this.state = {
show: false
};
}
updateState = () => {
this.setState({
show: !this.state.show
});
}
render() {
return (
<Child updateState={this.updateState} />
);
}
}
class Child extends React.Component {
handleClick = () => {
this.props.updateState();
}
render() {
return (
<button onClick={this.handleClick}>Test</button>
);
}
}