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.
相关问题
- How can I create this custom Bottom Navigation on
- How to toggle on Order in ReactJS
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Refreshing page gives Cannot GET /page_url in reac
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Why would we use useEffect without a dependency ar
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
You want to perform something after every time navigating to a component using
drawernavigator
orstacknavigator
; for this purposeNavigationEvents
fits better thancomponentDidMount
.Note : If you want to do the task every time after focusing on a component using drawer navigation or stack navigation then using
NavigationEvents
is better idea. But if you want to do the task once then you need to usecomponenetDidMount
.React-navigation keeps the component mounted even if you navigate between screens. You can use the component to react to those events :
More info about this component here.
In React, componentDidMount is called only when component is mounted.I think what you are trying to do is call your API on going back in StackNavigator. You can pass a callback function as parameter when you call navigate like this on Parent Screen:
And on Child Screen
If anyone coming here in 2019, try this:
Add the component to your render:
Now, this onDidFocus event will be triggered every time when the page comes to focus despite coming from goBack() or navigate.
If the upvoted syntax that uses NavigationEvents component is not working, you can try with this:
The React-navigation documentation explicitly described this case:
Now there is 3 solutions for that:
withNavigationFocus
higher-order-component<NavigationEvents />
componentwithNavigationFocus
higher-order-component<NavigationEvents />
componentuseFocusState hook
First install library
yarn add react-navigation-hooks
Here is my personal solution, using
onDidFocus()
andonWillFocus()
to render only when data has been fetched from your API: