ReactNative界面间的数据传递和页面刷新机制

假设场景: APP的首页的数据 需根据子页面的数据操作 进行数据刷新;或者有页面A,B。A页面跳转到B页面,然后在B页面中执行了某些操作后点击回退按钮到A 页面,A页面数据需要刷新。

react-native中事件监听是使用插件DeviceEventEmitter实现的,基本语法,如下所示 


1.设置监听 
在需要接受监听的地方进行添加监听,假如是A页面

import { DeviceEventEmitter } from 'react-native';
...
componentDidMount() {

    //收到监听

    DeviceEventEmitter.addListener('通知名称', (param) => {
        //收到通知后处理逻辑
        //eg 刷新数据    
    })
}
componentWillUnmount() {

    //移除监听

    DeviceEventEmitter.remove();
  }
...

2.触发监听

在需要出发监听页面加入以下代码

import { DeviceEventEmitter } from 'react-native';
...

 //执行某些操作后触发事件通知,param传递参数,参数也可不传递

 DeviceEventEmitter.emit('通知名称', param);

 

你可能感兴趣的:(react,native)