一、消息訂閱與發(fā)布
1-1、安裝
npm install pubsub-js
yarn add pubsub-js
1-2、使用
A組件文章來源:http://www.zghlxwxcb.cn/news/detail-578530.html
import PubSub from 'pubsub-js';
componentDidMount(){
// 消息訂閱
PubSub.subscribe('lq',(_,data)=>{
this.setState(data)
})
}
B組件文章來源地址http://www.zghlxwxcb.cn/news/detail-578530.html
import PubSub from 'pubsub-js';
PubSub.publish('lq',{isFirst:false,isLoading:true})
1-3、總結(jié)
1. 消息訂閱與發(fā)布適用于各種組件之間的通信
到了這里,關(guān)于React組件之間通信的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!