使用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, 完成! 赶紧试试吧! ~~

相关文章:

  • 2022-01-20
  • 2021-11-19
  • 2021-09-25
  • 2021-12-08
  • 2022-12-23
  • 2021-11-25
  • 2021-12-17
  • 2021-10-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-29
  • 2022-12-23
  • 2022-12-23
  • 2021-09-13
  • 2022-01-18
相关资源
相似解决方案