React Native之didFocus和didBlur

举报
chenyu 发表于 2021/07/27 00:38:38 2021/07/27
【摘要】 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 com...

1  didFocus和didBlur解释


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

 

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

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

 

 

2 测试代码


  
  1. import React from 'react';
  2. import { View, Text, Button} from 'react-native';
  3. import { createStackNavigator } from 'react-navigation';
  4. class HomeScreen extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. console.log("HomeScreen constructor start");
  8. this.didFocusListener = this.props.navigation.addListener(
  9. 'didFocus',
  10. (obj) => {console.log("HomeScreen didFocus start")}
  11. );
  12. this.didBlurListener = this.props.navigation.addListener(
  13. 'didBlur',
  14. (obj) => {console.log('HomeScreen didBlur start')}
  15. );
  16. }
  17. static navigationOptions = {
  18. title : 'HomeScreen',
  19. }
  20. componentDidMount = () => {
  21. console.log("HomeScreen componentDidMount start")
  22. }
  23. componentWillUnmount() {
  24. console.log("HomeScreen componentWillUnmount start")
  25. this.didFocusListener.remove();
  26. this.didBlurListener.remove();
  27. }
  28. render() {
  29. return (
  30. <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
  31. <Text>Home Screen</Text>
  32. <Button onPress={() => this.props.navigation.navigate('Details', {
  33. itemId:100,
  34. otherParam:'chenyu',
  35. })} title = "go to Details"/>
  36. <Button
  37. title="Go back"
  38. onPress={() => this.props.navigation.goBack()}
  39. />
  40. </View>
  41. );
  42. }
  43. }
  44. class DetailsScreen extends React.Component {
  45. constructor(props) {
  46. super(props);
  47. console.log("DetailsScreen constructor start");
  48. this.didFocusListener = this.props.navigation.addListener(
  49. 'didFocus',
  50. (obj) => {console.log("DetailsScreen didFocus start")}
  51. );
  52. this.didBlurListener = this.props.navigation.addListener(
  53. 'didBlur',
  54. (obj) => {console.log('DetailsScreen didBlur start')}
  55. );
  56. }
  57. static navigationOptions = ({navigation}) => {
  58. return {
  59. title : navigation.getParam('otherParam', 'no-values'),
  60. };
  61. };
  62. componentDidMount = () => {
  63. console.log("DetailsScreen componentDidMount start")
  64. }
  65. componentWillUnmount() {
  66. console.log("DetailsScreen componentWillUnmount start")
  67. this.didFocusListener.remove();
  68. this.didBlurListener.remove();
  69. }
  70. render() {
  71. const {navigation} = this.props;
  72. const itemId = navigation.getParam('itemId', 'no-values');
  73. const otherParam = navigation.getParam('otherParam', 'no-values');
  74. return (
  75. <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
  76. <Text>Details Screen</Text>
  77. <Text>itemId:{JSON.stringify(itemId)}</Text>
  78. <Text>otherParam:{JSON.stringify(otherParam)}</Text>
  79. <Button
  80. title="Go to Details... again"
  81. onPress={() => this.props.navigation.push('Details', {
  82. itemId: Math.floor(Math.random() * 100),
  83. })}
  84. />
  85. <Button
  86. title="Go to Home"
  87. onPress={() => this.props.navigation.navigate('Home')}
  88. />
  89. <Button
  90. title="Go back"
  91. onPress={() => this.props.navigation.goBack()}
  92. />
  93. <Button
  94. title="Go popToTop"
  95. onPress={() => this.props.navigation.popToTop()}
  96. />
  97. </View>
  98. );
  99. }
  100. }
  101. const RootStack = createStackNavigator(
  102. {
  103. Home: HomeScreen,
  104. Details: DetailsScreen,
  105. },
  106. {
  107. initialRouteName: 'Home',
  108. }
  109. );
  110. export default class App extends React.Component {
  111. constructor(props) {
  112. super(props);
  113. }
  114. render() {
  115. return <RootStack/>;
  116. }
  117. }

 

 

 

 

3 运行结果

2个页面分别如下

 

 

 

在控制台过来React Native命令

adb logcat | grep ReactNativeJS
 

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


  
  1. I/ReactNativeJS(21233): HomeScreen constructor start
  2. I/ReactNativeJS(21233): HomeScreen componentDidMount start
  3. I/ReactNativeJS(21233): HomeScreen didFocus start

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

 

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


  
  1. I/ReactNativeJS(21233): DetailsScreen constructor start
  2. I/ReactNativeJS(21233): DetailsScreen componentDidMount start
  3. I/ReactNativeJS(21233): HomeScreen didBlur start
  4. I/ReactNativeJS(21233): DetailsScreen didFocus start

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

 

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


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

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


  
  1. I/ReactNativeJS(21233): DetailsScreen constructor start
  2. I/ReactNativeJS(21233): DetailsScreen componentDidMount start
  3. I/ReactNativeJS(21233): HomeScreen didBlur start
  4. 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函数,然后离开当前页面才执行,也意味着,这个页面没有死但是去了另外一个页面才会调用,如果自己页面死了,就不会调用到这里.

 

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/83218639

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。