本文介绍了导航后不调用`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';

将组件添加到您的渲染中:

Add the component to your render:

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

现在,这个 onDidFocus 事件将在每次页面获得焦点时触发,尽管来自 goBack() 或导航.

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

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

08-18 13:56