React Native之didFocus和didBlur

1  didFocus和didBlur解释

didFocus - the screen focused (if there was a transition, the transition completed)didBlur - the screen unfocused (if there was a transition, the transition completed)

 

didFocus是指当前页面第一次加载的时候会调用一次

didBlur是指当前页面离开的时候会调用一次(前提是当前页面没有被销毁既没有执行componentWillUnmount()函数)

 

 

2 测试代码

import React from 'react';
import { View, Text, Button} from 'react-native';
import { createStackNavigator } from 'react-navigation';class HomeScreen extends React.Component {constructor(props) {super(props);console.log("HomeScreen constructor start");this.didFocusListener = this.props.navigation.addListener('didFocus',(obj) => {console.log("HomeScreen didFocus start")});this.didBlurListener = this.props.navigation.addListener('didBlur',(obj) => {console.log('HomeScreen didBlur start')});}static navigationOptions = {title : 'HomeScreen',}componentDidMount = () => {console.log("HomeScreen componentDidMount start")}componentWillUnmount() {console.log("HomeScreen componentWillUnmount start")this.didFocusListener.remove();this.didBlurListener.remove();}render() {return (<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}><Text>Home Screen</Text><Button onPress={() => this.props.navigation.navigate('Details', {itemId:100,otherParam:'chenyu',})} title = "go to Details"/><Buttontitle="Go back"onPress={() => this.props.navigation.goBack()}/></View>);}
}class DetailsScreen extends React.Component {constructor(props) {super(props);console.log("DetailsScreen constructor start");this.didFocusListener = this.props.navigation.addListener('didFocus',(obj) => {console.log("DetailsScreen didFocus start")});this.didBlurListener = this.props.navigation.addListener('didBlur',(obj) => {console.log('DetailsScreen didBlur start')});}static navigationOptions = ({navigation}) => {return {title : navigation.getParam('otherParam', 'no-values'),};};componentDidMount = () => {console.log("DetailsScreen componentDidMount start")}componentWillUnmount() {console.log("DetailsScreen componentWillUnmount start")this.didFocusListener.remove();this.didBlurListener.remove();}render() {const {navigation} = this.props;const itemId = navigation.getParam('itemId', 'no-values');const otherParam = navigation.getParam('otherParam', 'no-values');return (<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}><Text>Details Screen</Text><Text>itemId:{JSON.stringify(itemId)}</Text><Text>otherParam:{JSON.stringify(otherParam)}</Text><Buttontitle="Go to Details... again"onPress={() => this.props.navigation.push('Details', {itemId: Math.floor(Math.random() * 100),})}/><Buttontitle="Go to Home"onPress={() => this.props.navigation.navigate('Home')}/> <Buttontitle="Go back"onPress={() => this.props.navigation.goBack()}/><Buttontitle="Go popToTop"onPress={() => this.props.navigation.popToTop()}/></View>);}
}const RootStack = createStackNavigator({Home: HomeScreen,Details: DetailsScreen,},{initialRouteName: 'Home',}
);export default class App extends React.Component {constructor(props) {super(props);}render() {return <RootStack/>;}}

 

 

 

 

3 运行结果

2个页面分别如下

 

 

 

在控制台过来React Native命令

adb logcat | grep ReactNativeJS

1) 程序起来打印日志如下

I/ReactNativeJS(21233): HomeScreen constructor start
I/ReactNativeJS(21233): HomeScreen componentDidMount start
I/ReactNativeJS(21233): HomeScreen didFocus start

这里的didFocus start是在componentDidMount后面执行

 

2 ) 然后点击go to DETAILS按钮日志如下

I/ReactNativeJS(21233): DetailsScreen constructor start
I/ReactNativeJS(21233): DetailsScreen componentDidMount start
I/ReactNativeJS(21233): HomeScreen didBlur start
I/ReactNativeJS(21233): DetailsScreen didFocus start

然后执行了HomeScreen didBlur start,但是并没有执行HomeScreen componentWillUnmount start,因为页面还没有销毁,所以执行了HomeScreen didBlur start.

 

3 )然后在在第二个页面点击"GO BACK"或者按下返回键,日志打印如下

I/ReactNativeJS(21233): DetailsScreen componentWillUnmount start
I/ReactNativeJS(21233): HomeScreen didFocus start

发现没有,既然执行了componentWillUnmount函数,说明页面已经销毁,既然销毁了,就没有执行DetailsScreen didBlur start,因为前面的页面没有死,所以不会重新加载再次调用首页的constructor和componentDidMount方法.从前面日志打印

I/ReactNativeJS(21233): DetailsScreen constructor start
I/ReactNativeJS(21233): DetailsScreen componentDidMount start
I/ReactNativeJS(21233): HomeScreen didBlur start
I/ReactNativeJS(21233): DetailsScreen didFocus start

可以看出,另外一个页面执行新页面的constructor函数和componentDidMount函数才执行之前页面的didBlur start,所以估计这里是来不及执行页面就销毁了,所以没有打印DetailsScreen didBlur start.

4 )然后再次点击返回物理键日志如下

I/ReactNativeJS(23183): HomeScreen componentWillUnmount start

只调用了componentWillUnmount函数,所以页面销毁了,HomeScreen didBlur start来不及打印.

 

 

 

4 总结

didFocus只会在当前页面的constructor函数和componentDidMount函数后面执行

didBlur只会在当前页面没有调用componentWillUnmount函数,然后离开当前页面才执行,也意味着,这个页面没有死但是去了另外一个页面才会调用,如果自己页面死了,就不会调用到这里.

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/289117.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python语法详解_解析 Python3 基础语法

行与缩进 python最具特色的就是使用缩进来表示代码块&#xff0c;不需要使用大括号 {} 。 缩进的空格数是可变的&#xff0c;但是同一个代码块的语句必须包含相同的缩进空格数。实例如下&#xff1a; if True: print ("True") else: print ("False") Color…

ENVI扩展工具:利用波段运算修改NaN方法总结

NaN为Not a Number的缩写,在遥感图像中属于异常值。很多用户有修改NaN的需求,比如把0值修改为NaN,或把NaN修改为0值等。由于波段运算公式较为复杂,现归纳如下。 1. 直接利用波段运算进行 Ÿ 修改0值为NaN – float(b1)*b1/b1 Ÿ 修改特定值(250)为Na…

ASP.NET 6 中间件 - 介绍与基础

这是一个关于 .NET 6 中间件的系列文章。在这个系列中&#xff0c;我们将了解到什么是中间件&#xff0c;它能够做什么&#xff0c;以及我们为什么要使用它&#xff0c;并演示几种不同类型的中间件的实现。之后&#xff0c;我们会进一步了解中间件所在的管道&#xff0c;以及如…

数组的迭代数组里面每个对象添加属性值_JS数组和对象循环遍历的几种实现方式...

数组遍历1. 普通for循环let arr [1,2,3,4,5] for (let i 0; i < arr.length; i) {console.log(arr[i]) } // 输出结果 // 1 // 2 // 3 // 4 // 52. 优化普通for循环let arr [1,2,3,4,5] for(var j 0,len arr.length; j < len; j){console.log(arr[j]); }3. forEach循…

Blend4精选案例图解教程(三):一键拖拽

原文:Blend4精选案例图解教程&#xff08;三&#xff09;&#xff1a;一键拖拽拖拽效果&#xff0c;常规实现方法是定义MoveLeftDwon、MoveLeftUp、MouseMove事件&#xff0c;在Blend的世界里&#xff0c;实现对象的拖拽&#xff0c;可以不写一行代码&#xff0c;而且非常简单&…

redis学习之三配置文件redis.conf 的含义

摘自http://www.runoob.com/redis/redis-conf.html 安装redis之后的第一件事&#xff0c;我就开始配置密码&#xff0c;结果总是不生效&#xff0c;而我居然还没想到原因。今天突然用命令行设置了密码&#xff0c;居然可以了。然后info一下&#xff0c;看到配置文件位置才恍然大…

React Native之hardwareBackPress

1 hardwareBackPress 我们用hardwareBackPress来监听手机物理返回键 2 js那边常用写法 BackHandler.addEventListener(hardwareBackPress, this._back); 3 我们看下Android源代码分析是怎么触发到这里来的 1) ReactActivity.java里面的部分代码如下 Overridepublic void o…

python画气泡图_​用Python把图做的好看点:用Matplotlib画个好看的气泡图

我们继续来把简单的图形丢到极坐标&#xff0c;这次是气泡图和柱状图&#xff0c;临摹的对象是澎湃美数课这个图看起来很好看&#xff0c;原理其实很简单&#xff0c;把柱状图和气泡图从笛卡尔坐标系中转移到极坐标系中来就OK 我们开始本次的临摹吧 本期的主题如下&#xff1a;…

C#输入框InputBox问题

C#里面没有像VB一样的InputBox,但是像InputBox一样的控件在程序中很实用,这里有两种方法实现: 一、调用VB里的InputBox 首先添加引用Microsoft.VisualBasic,然后在命名空间中引用(using Microsoft.VisualBasic;)。 举个实例: using System; using System.Collections.Ge…

mysql为什么添加索引_当我添加新索引时,为什么MySQL中索引的基数保持不变?

如果表中只有1行,则索引的基数当然应为1.它只是计算唯一值的数量.如果您将索引视为基于存储桶的查找表(如散列),则基数是存储桶的数量.以下是它的工作原理&#xff1a;当您在一组列(a,b,c,d)上构建索引时,数据库将遍历表中的所有行,查看每行的4个列的有序四元组.假设你的表看起…

win32 注册表操作

创建键 RegCreateKeyEx int SetRecordVideoSavedDays(int newSavedDays)2 {3 HKEY hSubKey NULL;4 LONG lRet 0;5 DWORD dwType 0;6 int iRet 0;7 8 do 9 {10 if (newSavedDays < 0)11 {12 printf(&quo…

进程间通信(一)

1 消息队列 消息队列是消息的链接表 , 存放在内核中并由消息队列标识符标识。 m s g g e t用于创建一个新队列或打开一个现存的队列。 m s g s n d用于将新消息添加到队列尾端。 m s g r c v用于从队列中取消息。 调用的第一个函数通常是m s g g e t&#xff0c;其功能是打开一…

Asp.NET Core一个接口的多个实现如何基于当前HTTP请求注册

前言假设我们有三个Service类实现了同一接口&#xff0c;示例代码如下&#xff1a;public interface IService { } public class ServiceA : IService { } public class ServiceB : IService { } public class ServiceC : IService { }我们希望在运行时使用依赖注入指定其具体…

Loadrunner 接口测试方法

其实无论用那种测试方法&#xff0c;接口测试的原理是通过测试程序模拟客户端向服务器发送请求报文&#xff0c;服务器接收请求报文后对相应的报文做出处理然后再把应答报文发送给客户端&#xff0c;客户端接收应答报文这一个过程。一、基于通用http/html请求大家都知道LoadRun…

React Native之react-native bundle --platform android --dev false --entry-file index.js --bundle失败

1 问题 react native项目在assert目录下面生成index.android.bundle文件用下面的命令 react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/r…

pytorch forward_【Pytorch部署】TorchScript

TorchScript是什么&#xff1f;TorchScript - PyTorch master documentation​pytorch.orgTorchScript是一种从PyTorch代码创建可序列化和可优化模型的方法。任何TorchScript程序都可以从Python进程中保存&#xff0c;并加载到没有Python依赖的进程中。我们提供了一些工具来增量…

兼容ie8 rgba()用法

今天遇到了一个问题&#xff0c;要在一个页面中设置一个半透明的白色div。这个貌似不是难题&#xff0c;只需要给这个div设置如下的属性即可&#xff1a; background: rgba(255,255,255,.1); 但是要兼容到ie8。这个就有点蛋疼了。因为ie8不支持rgba()函数。下面我们总结一下rgb…

hdu水仙花

水仙花数Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 11 Accepted Submission(s) : 6 Problem Description 春天是鲜花的季节&#xff0c;水仙花就是其中最迷人的代表&#xff0c;数学上有个水仙花数&#xff…

python中的标识符能不能使用关键字_Python中的标识符不能使用关键字

Python中的标识符不能使用关键字答&#xff1a;√智慧职教: 检查客室座椅外观良好&#xff0c;确认?无破损答&#xff1a;坐垫 靠背关于投标报价时综合单价的确定&#xff0c;下列做法中正确的是()答&#xff1a;以项目特征描述为依据确定综合单价城市总体规划调查时&#xff…

C# WPF实战项目升级了

概述之前用Caliburn.Micro搭建的WPF实战项目&#xff0c;CM框架选用了 3.0.3&#xff0c;实际上CM框架目前最新版已经到4.0。173了&#xff0c;所有很有必须升级一下项目了. 本来打算把平台框架也直接升级到.NET 6 的&#xff0c;但是项目里面很多库不支持最新的平台版本&#…