react-native--->RN发送/接收事件机制

import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Platform,
    NativeAppEventEmitter,
    DeviceEventEmitter,
} from 'react-native';

//添加DeviceEventEmitter

componentDidMount() {
	if(Platform.OS=='android'){
	    this.subscription = DeviceEventEmitter.addListener('userNameDidChange',(userName) => {
        alert('通知');
    })
	}else {
	    this.subscription = NativeAppEventEmitter.addListener('userNameDidChange',(userName) => {
        alert('通知');
    })
	}

},

移除DeviceEventEmitter
componentWillUnmount() {
    // 移除
    this.subscription.remove();
},

发送通知
DeviceEventEmitter.emit('userNameDidChange', '通知来了');

OK, 完成!
posted @ 2017-10-27 10:21  技术-刘腾飞  阅读(1784)  评论(0编辑  收藏  举报