In the project, do not know whether you have encountered such a problem, for example, there are two pages, a, B. A page has a button click to jump to the B page, now there is a requirement is that I did something in the B page, and then click the Fallback button to go back to the a page, a page of the data also need to be refreshed. Well, if you are a friend of Android developers will know that there are many ways to solve such requirements, such as the life cycle of the activity can be used, but also the use of radio and so on, but if in the react native??
We can see the official website to know, react native life cycle is not for the page, but the component, what does it mean? In other words, this life cycle is the component's life cycle, not the page life cycle, the page in the jump, the page will be back to the corresponding life cycle method, but the component in the page jump will not necessarily callback the corresponding life cycle method, so react native, Using the life cycle as a way to refresh the page is not the best option. So react native there is a similar and broadcast such things. Yes, that's the thing: Deviceeventemitter.
To register in page a:
1 import {
2 DeviceEventEmitter
3} from ‘react-native’;
4
5
6 // Register this listening event
7 componentDidMount () {
8 DeviceEventEmitter.addListener (‘xxxName’, Function);
9 };
10
11
12 // Remove the component when it is destroyed
13 componentWillUnmount () {
14 DeviceEventEmitter.remove ();
15};
It can be used on page B:
1 import {
2 DeviceEventEmitter
3} from ‘react-native’;
4
5 // Call event notification param refers to the corresponding parameters passed
6 DeviceEventEmitter.emit (‘xxxName’, param);
In this way, we realized the refresh of the page.
react native refresh mechanism-notification