react native不同组件间传递值

事件的订阅与发送:DeviceEventEmitter

注册事件监听

import {
  DeviceEventEmitter,
} from 'react-native';

class XiFan extends Component {
    componentDidMount(){
        this.subscription = DeviceEventEmitter.addListener('dataChange',this._onListenerCallback.bind(this));
      }

      componentWillUnmount(){
        this.subscription.remove();
      }

    _onListenerCallback(params){
        console.log('params = '+ params);
      };
  }

在另一个组件中,需要发送通知时,调用:

DeviceEventEmitter.emit('dataChange',params);

你可能感兴趣的:(React,Native)