事件总线 event bus

事件总线 event bus

 

 

import React, { PureComponent } from 'react' import {EventEmitter} from 'events'; //  yarn add events

// 事件总线 event bus const eventBus = new EventEmitter();
class Home extends PureComponent{
    componentDidMount(){         eventBus.addListener('sayHello',this.handleSayHelloListener)     }
    componentWillUnmount(){         eventBus.removeListener('sayHello',this.handleSayHelloListener);     }
    handleSayHelloListener(...args){         console.log(args);     }
    render() {         return (             <div>                 Home             </div>         )     } }

class Profile extends PureComponent{     render() {         return (             <div>                 Profile                <button onClick={e =>this.emmitEvent()}>点击的Profile按钮</button>              </div>         )     }
    emmitEvent(){         // eventBus.emit("sayHello","hello home",'123')         // eventBus.emit("sayHello","hello home",'123',{         //     id:1,         //     age:25         // })
        eventBus.emit("sayHello",{             id:1,             age:25         })     } }
export default class App extends PureComponent {     render() {         return (             <div>                 <Home />                 <Profile />             </div>         )     } }
上一篇:SSTI模板注入/python代码注入一点总结


下一篇:React(render-props和高阶组件)