Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`componentDidMount()` function is not called after navigation

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.

like image 830
hitttt Avatar asked Dec 29 '17 05:12

hitttt


People also ask

Is componentDidMount called after every render?

There is no call to ComponentDidMount. It is only called once after the initial render.

Can I call a function in componentDidMount?

So, after the component is rendered correctly, componentDidMount() function is called and that call getData() function.

Is componentDidMount called after state change?

It is called only once after the first render.

Is componentDidUpdate called after componentDidMount?

The componentDidUpdate()is called after componentDidMount() and can be useful to perform some action when the state of the component changes. Parameters: Following are the parameter used in this function: prevProps: Previous props passed to the component. prevState: Previous state of the component.


1 Answers

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')} /> 

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

like image 143
Jithesh Kt Avatar answered Oct 17 '22 06:10

Jithesh Kt