1. 利用react-native 事件DeviceEventEmitter 监听广播
应用场景:
- 表单提交页面, A页面跳转到B页面选人, 然后返回A页面, 需要将B页面选择的数据传回A页面。
- 多个多媒体来回切换播放,暂停后二次继续播放等问题。
代码如下:
A页面
componentDidMount() { // 利用DeviceEventEmitter 监听 concactAdd事件 this.subscription = DeviceEventEmitter.addListener('concactAdd', (dic) => {// dic 为触发事件回传回来的数据 // 接收到 update 页发送的通知,后进行的操作内容 if (dic.approver_list) { this.setState((preState: Object) => { this.updateInputValue(preState.approver_list.concat(dic.approver_list), 'approver_list'); return { approver_list: preState.approver_list.concat(dic.approver_list) }; }); } if (dic.observer_list) { this.setState((preState: Object) => { this.updateInputValue(preState.observer_list.concat(dic.observer_list), 'observer_list'); return { observer_list: preState.observer_list.concat(dic.observer_list) }; }); } }); ... componentWillUnmount() { this.subscription.remove(); }
B页面
// 触发concactAdd事件广播 handleOk = (names: []) => { const { field } = this.props; DeviceEventEmitter.emit('concactAdd', { [field]: names }); }
2. 用react-navigation提供的路由之间
A页面
// 定义路由跳转函数 cb表示需要传递的回调函数 export const navigateToLinkman = (cb: Function, type"htmlcode">handleToUser = () => { ... navigation.state.params.cb(user.email, group); ... }3. 利用react-navigation 提供的路由事件监听触发事件
在A页面路由失去焦点的时候触发该事件
componentDidMount() { this.props.navigation.addListener('didBlur', (payload) => { if (this.modalView) this.modalView.close(); }); }那么问题来了, 为何不在页面卸载(componentWillunmount)的时候触发该事件?
如果不了解react-native和react-navigation, 会很困惑, A页面卸载了, 为什么还能接收到来自B页面的数据或者事件, 原因是: react-navigation中, A页面跳转到B页面, A页面没有卸载, 只是在它提供的路由栈中堆积,例如A跳转到B中, A页面不执行componentWillunmount,当每一个路由pop掉的时候才会执行componentWillunmount, 卸载掉当前页面。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]
- 曼丽2021《女人三十2雾之恋》HQCD[WAV+CUE]
- 隔壁老樊.2019-我曾【好听音乐】【FLAC分轨】
- 高林生.1993-牵挂你的人是我【白天鹅】【WAV+CUE】
- 洪卓立.2012-找个懂我的女孩(国专)【英皇娱乐】【WAV+CUE】
- 曼丽2022《女人三十3晚秋》HQCD[WAV+CUE]
- 曼丽2018《女人三十4冬天》HQCD[WAV+CUE]
- 柏菲-《好歌30年壹》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 大自然音乐系列《水舞》[WAV/CUE/分轨][424.3MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]