本文介绍了导航后不调用componentDidMount()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用stackNavigator在屏幕之间导航.我在第二个活动中在componentDidMount()函数中调用了两个API.当我第一次加载它时,它会成功加载.然后,我按后退"按钮返回到第一个活动.然后,如果我再次进行第二次活动,则不会调用API,并且会出现渲染错误.我无法为此找到任何解决方案.任何建议,将不胜感激.

I am using stackNavigator for navigating between screens. I am calling two API's in componentDidMount() function in my second activity. When i load it first time, it gets loaded successfully. Then i press back button to go back to first activity. Then, if i am again going to second activity, the APIs are not called and I get render error. I am not able to find any solution for this. Any suggestions would be appreciated.

推荐答案

如果有人在2019年到这里来,请尝试以下操作:

If anyone coming here in 2019, try this:

import {NavigationEvents} from 'react-navigation';

将组件添加到渲染中:

<NavigationEvents onDidFocus={() => console.log('I am triggered')} />

现在,尽管页面来自goBack()或导航,但每次页面聚焦时都会触发此onDidFocus事件.

Now, this onDidFocus event will be triggered every time when the page comes to focus despite coming from goBack() or navigate.

这篇关于导航后不调用componentDidMount()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 22:38