javascript - React Native:当您滑回已安装的屏幕时如何触发事件?
问题描述
在我的 React Native 应用程序中,我有一个想要在某个屏幕出现时触发的事件。我将事件放入componentDidMount
,因此它会在屏幕首次加载时触发,但如果我离开屏幕并且向左滑动以返回屏幕,则屏幕已经安装,因此不会触发事件。
我应该如何处理这个?
解决方案
如果您正在使用react-navigation
,您可以执行此操作componentDidMount
以在已安装的屏幕上触发事件。
componentDidMount() {
const {navigation} = this.props;
this.onFocusTrigger = navigation.addListener('focus', () => {
// trigger your event
});
}
除此之外'focus'
,您还可以根据需要使用以下内容。
willFocus
didFocus
didBlur
willBlur
您需要像这样在 componentWillUnmount 中取消订阅侦听器
componentWillUnmount() {
this.onFocusTrigger();
}
推荐阅读
- sql - SQL(BigQuery) 中 2020 年 5 月 1 日至 2021 年 4 月 30 日之间一周中每一天的计数
- python - 如何使用 Plotly Python 绘制 Mesh3D 子图
- json - 有没有办法快速转换 Json
到 bson 能够将其保存到 mongo 吗? - vespa - 要跟踪哪些指标以确定是否需要添加资源
- image - 使用内在和外在参数将正面图像转换为鸟瞰图
- keras - 在 Tensorflow 中创建自定义 RNN 单元时出错
- nestjs - 如何在@nestjs/mongoose 模式中设置枚举
- c# - 我可以在单个 asp net core 5 项目中结合 react 和 webapi 吗?
- html - 带有选择所有复选框的多选下拉菜单
- javascript - 如何将变量分配为对象内部的值?