module.exports= class APP extends React.Component {
constructor(props){
super(props);
this.state = {
key1:\'key1\',
key2:\'key2\'
};
rend
Using Props like this:
Parent:
<Parent>
<Child onChange={this.change} />
</Parent>
Child:
<button onclick={this.props.onChange('It Changed')} />
With this you can just do whatever you want in your parent.
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>
);
}
}
class Parent extends Component{
constructor(props){
super(props);
this.state={value:''};
this.getData=this.getData.bind(this);
}
getData(val){
console.log(val);
this.setState({
value:val
});
}
render(){
const {value}=this.state
return(
<View>
<Screen sendData={this.getData}/>
<View>
<Text>
{this.state.value};
</Text>
</View>
</View>
)
}
}
export default Parent;
CHILD CLASS:
class Child extends Component{
componentWillMount(){
this.props.sendData('data');
}
render(){
return(
<View>
</View>
)
}
}
export default Child;
Like you do with React.
You have to exchange information through props, or use a library like Redux.
This can be achieved by two ways:
Parent Component:
//Needs argument
const addGoalHandler = goalTitle => {
// code for updating state
}
<GoalInput onAddGoal={this.addGoalHandler} />
Child Component:
Way 1: Using Vanilla Javascript
<Button
title="ADD"
onPress={props.onAddGoal.bind(this, enteredGoal)}
/>
Way 2: Using Arrow Function
<Button
title="ADD"
onPress={() => { props.onAddGoal(enteredGoal) } }
/>
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);