I need to call the api only when we back from the next screen to previous screen and it should not be called on the first time when we go to the screen in react native.
Question posted in React native
The official React Native documentation can be found here.
The official React Native documentation can be found here.
2
Answers
The approach I use in such situations is to pass a route param that would trigger that particular api on the parent screen. I am assuming you are using React Native Navigation V6. In that case the child would have code like this:
and in your parent element the code will look like:
So when you land on this screen from anywhere else then the callAPI is undefined and so the api call wont trigger but when you navigate back from the child screen then the api is triggered because callAPI is true and you get your results.
You can pass params from your 2nd screen like ,
And in the first screen you can do it like this using useEffect hook,
This useEffect will be called