我想将 setState
从 A
组件设置为 B
组件。 A
和 B
是不同的 JS 文件。我尝试将 B
导入到 A
并访问 B
内的函数。还有将B
中的函数设为静态,然后才发现静态函数没有实例,所以无法访问静态中的this
。
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);
最佳答案
关于javascript - React Native - 不同JS文件之间的setState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46825849/