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