2
react组件传参(消息订阅与发布机制)
source link: https://segmentfault.com/a/1190000039855155
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
react组件传参(消息订阅与发布机制)
发布于 刚刚
yarn add pubsub-js
import PubSub from "pubsub-js";
usersList
为订阅名称,msg
为发布接收的名称,data
为发布传的数据
PubSub.subscribe("usersList", (msg, data) => {
console.log(msg, data);
});
PubSub.publish("usersList", {id:1,html_url:'',login:this.searchName.value});
var token = PubSub.subscribe("usersList", (msg, data) => {
console.log(msg, data);
});
PubSub.unsubscribe(token);
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK