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

时间:2016-08-31 07:40:02

标签: javascript reactjs react-native react-router react-redux

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

我有一个tabbar,一个Tab列出数据,其他tab可以编辑数据。编辑数据后,当我转到第一个标签时,它仍会显示旧数据。 一种方法可能是每次选择时重新加载选项卡。

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

有可能吗?有什么解决方案吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案, 我的组件很少。组件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'
      });
    }