En ocasiones necesitamos resolver datos de HIJO a PADRE en REACT, aqui la solucion
Parent.js
import React, { Component } from 'react';
import { Text, View } from 'react-native';
import Child from './Child.js';
export default class Parent extends Component {
constructor() {
super();
this.state = {
result: 0
};
}
getResponse(result){
this.setState({result});
}
render(){
return (
<View>
<Text>{this.state.result}</Text>
<Child num={2} callback={this.getResponse.bind(this)}>
</View>
);
}
}
Child.js
import React, { Component } from 'react';
import { Button } from 'react-native';
export default class Child extends Component {
calc(){
this.props.callback(this.props.num * 2);
}
render(){
return (<Button onPress={() => this.calc()} title="Calc" />)
}
}