React native refresh mechanism----notification

Source: Internet
Author: User



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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.