ich den Kontext all meine Hauptkomponente von meinem Kind Komponente auf gesetzt und es funktioniert gut, aber ich weiß nicht, ob dies richtig istIst dies der richtige Weg, um den Zustand der übergeordneten Komponente in react.js
das ist meine Hauptkomponente
import Child from "./apps/child";
export default class NewTest extends Component {
constructor(){
super();
this.state={
one:1,
}
}
render() {
return (
<View style={styles.container}>
<Text>{this.state.one}</Text>
<Child root={this}/> //<----- Here i set all the context of my main Component to the child component
<TouchableOpacity onPress={()=>console.log(this.state.one)}>
<Text>Touch</Text>
</TouchableOpacity>
</View>
);
}
}
und das ist mein Kind Komponente
export default class Child extends Component{
constructor(props){
super(props);
this.parent=this.props.root;
}
render(){
return(
<View>
<TouchableOpacity onPress={()=>{
this.parent.setState({one:this.parent.state.one+1}) // <- if you see here i change the state of the parent, and it work fine
}}>
<Text>Sum to Parent</Text>
</TouchableOpacity>
</View>
)
}
}
All dies funktioniert, aber dann ist dies der richtige Weg, es zu tun?