react-native - React Native 根据所选按钮显示在应用程序中的更改数据
问题描述
在我的应用程序顶部,我有 3 个按钮:Load1、Load2、Add。
Load1 => 加载data1并显示内容
Load2 => 加载data2并显示内容
添加 => 添加数据
3 个按钮:
class Comp1 extends Component {
...
renderMainPage() {
switch (this.state.selectedButton) {
case 'load1':
return <ListComp status="load1" />;
case 'closed':
return <ListComp status="load2" />;
case 'add':
return <AddComp status="add" />;
default:
break;
}
}
...
render() {
<View style={{ flex: 1 }}>
<Button onPress={() => this.setState({ selectedButton: 'load1' })} > <Text>Load1</Text>
</Button>
<Button onPress={() => this.setState({ selectedButton: 'load2' })} > <Text>Load2</Text>
</Button>
<Button onPress={() => this.setState({ selectedButton: 'add' })} > <Text>Add</Text>
</Button>
{this.renderMainPage()}
</View>
}
}
按钮工作正常并加载正确的内容。ListComp:
componentDidMount() {
this.getData();
}
getData = async () => {
if (this.props.status === 'load1') {
await this.props.getLoad1();
} else if (this.props.status === 'load2') {
await this.props.getLoad2();
}
this.setState({
myData: this.props.data
});
};
那么 AddComp 只是一个表单组件。它默认加载“Load1”数据。然后,我单击“Load2”它获取 load2 数据。然后,我单击“添加”,然后单击“Load1”。它转到this.props.getLoad1()
,但没有更新“myData”。
如果我一直在“Load1”和“Load2”之间切换,它工作得很好。
解决方案
推荐阅读
- javascript - 为什么变量总是输出1,即使它在Javascript中递增
- c++ - 当使用模板支持函子作为参数时,我应该使用什么限定符?
- android - Android MVVM:如何在 ViewModel 中通过 LiveData 订阅网络调用
- java - Spring Boot 查询错误的表
- angular - Angular 5 SelectControlValueAccessor 实现错误
- bash - 使用 awk 打印最大的字母数字 grep 结果并按顺序创建变量 next
- java - Mybatis中一对一嵌套查询如何嵌套一对多查询
- php - Laravel 为 foreach 提供的参数无效
- java - onClick() 中的 setVisibility() 使应用程序崩溃
- ios - Swift 4 与 unity3d 5.6.6f2 集成