在屏幕之间导航但构造函数没有调用

编程入门 行业动态 更新时间:2024-10-11 05:28:08
本文介绍了在屏幕之间导航但构造函数没有调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的应用中有多个屏幕,我使用 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.

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

For more information check the docs: reactnavigation/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)

更多推荐

在屏幕之间导航但构造函数没有调用

本文发布于:2023-08-06 14:41:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1313035.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   屏幕

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!