美文网首页
DeviceEventEmitter的使用

DeviceEventEmitter的使用

作者: kim_xx | 来源:发表于2019-03-28 09:48 被阅读0次

    react-native 中传值的方式之一。

    需要接收通知的组件

    // 1.导入模块
    import { DeviceEventEmitter } from 'react-native';
    
    
    // 2.添加监听 并实现回调
        componentDidMount() {
            this.subscription = DeviceEventEmitter.addListener('StartAnswer', () => {
              // do something
            })
        }
        
    // 3.移除监听
        componentWillUnmount() {
            this.subscription.remove()
        }
    

    需要发送通知的组件

    import { DeviceEventEmitter } from 'react-native';
    
    // 在需要发送通知的地方
    DeviceEventEmitter.emit("StartAnswer")
    

    相关文章

      网友评论

          本文标题:DeviceEventEmitter的使用

          本文链接:https://www.haomeiwen.com/subject/ekpfbqtx.html