React Native:运行 一个组件中的函数来自另一个组件

React Native: Run a function in a component from another component

我不会将数据传递给另一个组件,也不会从一个组件访问功能并 运行 在另一个组件中访问它。组件 A 从数据库中获取一些数据。组件 B 编辑数据库中的一些数据。当我在组件 B 中编辑一些数据时,我需要重新执行组件 A 中的函数(获取数据)。我需要更新组件 A 中的列表。


我有一个标签栏,一个标签列出数据,另一个标签可以编辑数据。在我编辑数据后,当我转到第一个选项卡时,它仍然显示旧数据。 一种方法是每次选择时重新加载选项卡。

我正在使用 react-native-router-flux ,但我还是新手。

可能吗?请问有什么解决办法吗?

我找到了解决方案, 我的组件很少。组件A、B、C,其实每个组件都是一个tab(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'
      });
    }