在屏幕之间导航但构造函数没有调用 [英] Navigation between Screens but constructor is not calling

查看:36
本文介绍了在屏幕之间导航但构造函数没有调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有多个屏幕,我使用 CreatStackNavigator 进行导航.如果我第二次访问屏幕,则不会调用构造函数.

I have multiple screens in my app, I used CreatStackNavigator for navigation. If I visit the screen second time the constructor is not getting called.

假设我有四个屏幕 A,B,C &D,目前我在 A.

Let suppose I have four screen A,B,C & D and currently I am at A.

然后我分别去 C 和 D 屏幕.

Then I go to C and then D screens respectively.

现在,如果我再次单击 C,则不会调用 C 的构造函数.

Now if I clicked on C again then the constructor of C is not getting called.

我也使用了 ComponentReceivedMount() 但它没有用.

I used ComponentReceivedMount() also but it didn't worked.

我的代码是:-

  constructor(props) {
    super(props);
    ToastAndroid.show('heeeelll', ToastAndroid.LONG);
    this.state = {
        drawerOpen: false,
        op: 1,
        cl: '#ffffff',
        swiper: this.renderSwpier,
        showSwiper: false,
        ftc: '#2c3554',
        stc: '#c8c8c8',
        firstTopcolor: 1,
        secondTopColor: 0,
        showview: true,
    }
}

提前致谢.

推荐答案

React 导航有自己的导航生命周期 api 可用,它允许您确定屏幕何时变为活动状态.

React navigation has its own navigation lifecycle api available, which allows you to determine when a screen has become active.

有关更多信息,请查看文档:https://reactnavigation.org/docs/en/navigation-lifecycle.html

For more information check the docs: https://reactnavigation.org/docs/en/navigation-lifecycle.html

withNavigationFocus HOC 提供了一个 isFocused 道具,它允许您确定屏幕是否可见.

The withNavigationFocus HOC provides an isFocused prop which allows you to determine is a screen is visible.

示例:

import { withNavigationFocus } from 'react-navigation';

class YourScreen extends React.Component {

  render() {
     ...
  }

  componentDidUpdate(prevProps) {
    if (this.props.isFocused && !prevProps.isFocused) {
      // Screen has now come into focus, perform your tasks here! 
    }
  }

}

export default withNavigationFocus(YourScreen)

这篇关于在屏幕之间导航但构造函数没有调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆