setState
A
구성 요소에서 B
구성 요소로 보내고 싶습니다. A
및 B
은 다른 JS 파일입니다. B
을 A
으로 가져오고 B
안에있는 함수에 액세스하려고했습니다. 또한 B
에 정적으로 함수를 만들었습니다. 정적 함수가 없으므로 인스턴스가 없어서 정적으로 this
에 액세스 할 수 없었습니다.React Native - 다른 JS 파일간에 setState
A.js
import B from '../B';
class A extends React.Component {
ChangeBContent(){
B.SetContent();
}
render(){
return(
<View>
<SpeicalBtn onPress={()=> this.ChangeBContent()}/>
</View>
);
}
}
module.exports = A;
AppRegistry.registerComponent('myApp',() => A);
B.js는
class B extends React.Component {
constructor(props) {
super(props);
this.state = {
content:''
}
}
SetContent(){
this.setState({content:'123'});
}
render(){
return(
<View>
<Text>{this.state.content}</Text>
</View>
);
}
}
module.exports = B;
AppRegistry.registerComponent('myApp',() => B);
"정적"메서드'B.SetContent();를 호출하려고하는 대신 오히려'B' 인스턴스를 얻고 인스턴스를 통해 메서드를 호출하는 방법을 찾아야한다. . – Panther