此机制可用于 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);

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
  • 2021-08-09
相关资源
相似解决方案