react 中 EventEmitter 事件总线机制

此机制可用于 react 中兄弟组件中的通信

npm install events -S

事件总线:

// eventBus.js

import {EventEmitter} from 'events';
export default new EventEmitter();

监听:

import Bus from './eventBus'

Bus.addListener('changeSiblingsData', (msg) => {
  this.setState({
    bus: msg,
  });
  console.log(msg);
});

触发:

import Bus from './eventBus'

Bus.emit('changeSiblingsData', msg);

 

转载于:https://www.cnblogs.com/xiaoyaoxingchen/p/11093061.html

你可能感兴趣的:(react 中 EventEmitter 事件总线机制)