【问题标题】:React Native: Run a function in a component from another componentReact Native:从另一个组件运行一个组件中的函数
【发布时间】:2016-08-31 07:40:01
【问题描述】:
我不会将数据传递给另一个组件,或者从一个组件访问函数并在另一个组件中运行它。组件 A 从数据库中获取一些数据。组件 B 编辑数据库中的一些数据。当我在组件 B 中编辑一些数据时,我需要重新执行组件 A 中的函数(获取数据)。我需要更新组件 A 中的列表。
我有一个标签栏,一个标签列出数据,另一个标签可以编辑数据。在我编辑数据后,当我转到第一个选项卡时,它仍然显示旧数据。
一种方法是每次选择选项卡时重新加载选项卡。
我正在使用 react-native-router-flux ,但我还是新手。
有可能吗?请问有什么解决办法吗?
【问题讨论】:
标签:
javascript
reactjs
react-native
react-router
react-redux
【解决方案1】:
我找到了解决方案,
我的组件很少。组件 A、B 和 C。实际上每个组件都是一个选项卡(iOS 中的 TabBar)
每当用户对组件 C 进行一些更改时,我都需要更新组件 A 和 B。
在组件 C 中,当用户进行更改时,我会像这样调用 Actions.refresh :
/// Component C
updated(){
//// Update database
Actions.componentA();
Actions.refresh({somekey: 'True'});
Actions.componentB();
Actions.refresh({somekey: 'True'});
Actions.componentC();
Actions.refresh({somekey: 'True'});
}
/// So i first call Actions.componentA then i call Actions.refresh to send some props to each component.
/// in each component i added componentwillreceive update. when it receives new props, i'm calling the fetch function again. and setting some random state to make the component reload.
componentWillReceiveProps() {
this.fetchData();
this.setState({
somekey: 'yes'
});
}