惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
Help Net Security
Help Net Security
P
Proofpoint News Feed
J
Java Code Geeks
爱范儿
爱范儿
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
Google DeepMind News
Google DeepMind News
H
Help Net Security
G
Google Developers Blog
Jina AI
Jina AI
Vercel News
Vercel News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
Lohrmann on Cybersecurity
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
GbyAI
GbyAI
B
Blog
O
OpenAI News
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
Cisco Blogs
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
AI
AI
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale

博客园 - 折翼的飞鸟

React中 state值根据props传入值变化的静态方法 getDerivedStateFromProps 微信小程序分享图片显示自定义内容 Taro4.x 引入 taro-ui 组件库 Taro4.x引入redux报错:TypeError: middleware is not a function uniapp+vue3 微信小程序使用mqtt通信 uniapp+vue3 在App.vue中如何设置全局调用方法 Taro 在页面中import包、组件、样式文件的顺序自定义规则 Taro优化VirtualList虚拟列表组件 Taro 引入moment.js打包过大 Taro 打包体积分析 Taro 插件引入 Taro: chooseLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json/ext.json Taro 支付宝小程序Page页获取小程序启动时的参数 Taro 主包vendors.js文件过大 Taro自定义Tabbar Taro 微信转支付宝小程序: 问题八 TypeError: Function(...) is not a function Taro微信转支付宝小程序:问题七 引入的文件图片文件名不能有@符号 Taro微信转支付宝小程序:问题六 同时编译微信小程序和支付宝小程序 Taro微信转支付宝小程序:问题五 编译时没有报错,工具中却莫名奇妙的错误提示
React:消息订阅(subscribe)-发布(publish)机制
折翼的飞鸟 · 2023-02-08 · via 博客园 - 折翼的飞鸟

发布类似触发事件, 订阅类似监听事件

使用这种方式的好处是,兄弟组件间通信不必再像使用props那种通过父组件来通信,多层组件之间通信也不必在一层一层的传递, 直接在触发事件的组件中发布消息 监听组件中订阅消息即可;

    1、工具库: PubSubJS

    2、下载: npm install pubsub-js --save 

    3、使用:

import PubSub from 'pubsub-js' //引入
PubSub.publish('delete', data) //发布消息
PubSub.subscribe('delete', function(data){ }); //订阅

示例:

import { Component } from 'react' 
import PubSub from "pubsub-js"; 
class App extends Component {
 
  SubscribeList = ['POSErrorMessage'];  // 订阅消息集合

  componentDidMount () {
// 开启订阅消息
this.SubscribeList.forEach(item => {
PubSub.subscribe(item, this.subscribeCallback); // 开启订阅
}) } componentWillUnmount() {
this.SubscribeList.forEach(item => { // 取消消息订阅 PubSub.unsubscribe(item); }) } componentDidShow () { // 开启订阅消息加载等待进度, componentDidMount 添加消息订阅 this.SubscribeList.forEach(item => { PubSub.subscribe(item, this.subsribeCallback); }) }/** * 订阅的回调函数 @subscribeName 订阅的消息名称, @data 数据 * */ subsribeCallback = (subscribeName, data) => { if (subscribeName === 'POSErrorMessage') { } } // 在 App 类中的 render() 函数没有实际作用 // 请勿修改此函数 render () { return ( <div></div> ) } } export default App

发布消息:

import PubSub from "pubsub-js";
PubSub.publish('POSErrorMessage', '消息内容') //发布消息

注意: 页面卸载得时候记得卸载unsubscribe取消订阅,否则消息还是会被监听