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, 完成!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM