Search code examples
reactjsreact-nativereact-navigationreact-navigation-stack

Is there a way to navigate depending on the last screen the user has navigated from in react native?


I am currently developing an e-commerce app with react-native but i came across this problem.

I have four screens: A, B, C, D.
A can navigate to C,
B can navigate to C,
C can navigate to A or D.

On screen C, i have a button that navigates to either A or D depending on this condition: If the user has navigated from A, i want them to be navigated back to A. If the user has navigated from B, i want them to be navigated to D. Is there a proper way to handle this? I thought of sending the screen information to C as props but that sounded way too primitive. What i am asking looks like this:

if(navigation.history.last === A){
    navigation.navigate("A")
}
else{
    navigation.navigate("D")
}

Any answer will be appreciated, thanks in advance!


Solution

  • According to documentation, there is a history property for navigation but it is used for tabs and drawers so if you would like to achieve this kind of behaviour, my suggestion would be to send a parameter when you are navigating like examples stated in docs