繁体   English   中英

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。实际上每个组件都是一个选项卡(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' }); } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM