react-native--->RN发送/接收事件机制
2024-09-06 17:28:35
import {
AppRegistry,
StyleSheet,
Text,
View,
Platform,
NativeAppEventEmitter,
DeviceEventEmitter,
} from 'react-native';
//添加DeviceEventEmitter
componentDidMount() {
if(Platform.OS=='android'){
this.subscription = DeviceEventEmitter.addListener('userNameDidChange',(userName) => {
alert('通知');
})
}else {
this.subscription = NativeAppEventEmitter.addListener('userNameDidChange',(userName) => {
alert('通知');
})
}
},
移除DeviceEventEmitter
componentWillUnmount() {
// 移除
this.subscription.remove();
},
发送通知
DeviceEventEmitter.emit('userNameDidChange', '通知来了');
OK, 完成!
最新文章
- PostgreSQL-PL/pgSQL-cursor,loop
- SQL SQL语句的增删改查
- 在VFP6中模拟CursorAdapter的功能
- 将 List<;UserInfo>;中的对象按照UserInfo.name进行分组
- underscore源码阅读记录
- php动态获取函数参数
- BZOJ3613 南园满地堆轻絮-二分法
- Node.js实践第一天
- ACboy needs your help again!
- <;script src=";xxx.php";>;<;/script>;
- H3C无线路由器安装与设置
- python 操作MongoDB
- 【Zabbix】Zabbix Server自动发现
- div的onclick事件怎么失效了?
- 7.4 electirc.c -- 计算电费
- Caused by: java.io.FileNotFoundException: velocity.log (No such file or directory)
- SDK安装详解以及adb的来源
- linux下搭建SVN
- CSS快速入门-属性和伪类
- 如何用有NFC功能的手机微信给公交卡充值?入口在那里?