简体   繁体   中英

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

I'm not going to pass data to another component, or access functions from a component and run it in another. Component A takes some data from database. Component B edits some data in Database. When i edit some data in Component B, i need function (fetch data) in component A to be re-executed. i need to update the list in component A.


I have a tabbar, one Tab lists data, other tab can edit data. after i edit the data, when i go to first tab, it's still shows the old data. One approach could be to re-load the tab every times it's selected.

I'm using react-native-router-flux , but i'm still new to it.

Is it possible? any solutions please?

I found the solution, I have few Components. Component A, B and C. actually each component is a tab (TabBar in iOS)

Whenever user makes some changes in Component C, i need to update component A and B.

In component C, when users makes the change, i'm calling Actions.refresh like this :

 /// 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' }); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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