내 기본 구성 요소의 모든 컨텍스트를 내 하위 구성 요소로 설정했으나 정상적으로 작동하는지는 모르겠지만 이것이 맞는지는 알 수 없습니다react.js에서 상위 구성 요소의 상태를 올바르게 설정하는 방법입니까
이 내 주요 구성 요소
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>
);
}
}
이며,이 내 아이 컴퍼넌트
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>
)
}
}
이 모든 작품이지만,이 그것을 할 수있는 올바른 방법인가?