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