React Native学习之DeviceEventEmitter传值

 使用DeviceEventEmitter前需添加

import {
     AppRegistry,
     StyleSheet,
     Text,
     View,
     DeviceEventEmitter
 } form 'react-native';

 

 添加DeviceEventEmitter

componentDidMount() {
     this.subscription = DeviceEventEmitter.addListener('userNameDidChange',(userName) =>{
          alert('通知');
     })
},

 

 移除DeviceEventEmitter

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

 

 发送通知

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

 

 OK, 完成! 赶紧试试吧! ~~

posted @ 2016-10-13 17:23  Milo_D  阅读(13656)  评论(1编辑  收藏  举报