NextJS、React中跨组件消息事件通知
- 创建event-bus.ts
import { EventEmitter } from 'events'
export default new EventEmitter()
- 设置监听
useEffect(() => {
EventBus.on("test_event", handleGenerateTopic);//监听事件总线
return () => {
EventBus.removeListener("test_event", handleGenerateTopic);//移出事件总线
}
}, []);
const handleGenerateTopic = (content: string) => {
}
- 发送事件
EventBus.emit("test_event", "hello")