I'm using react-navigation
as my navigation lib, and I wonder how can I change the 'back' button (that is added automatically by react-navigation
) functionality for a specific screen?
I mean instead of going one step in the stack of screens I want it to go 2 steps back in stack. or do it manually by giving it a screen name (again only on one component).
thanks.
Consider, that you have 3 screens A, B, C respectively. So, the default functionality of the back button in StackNavigator is:- If you press the Back Button on screen C, it will take you to the previous screen i.e, screen B. To override that you could do something like this on screen C:-
You can override back button for a specific screen with
navigationOptions
of that screen. You can read more info about overriding back button inreact-navigation
docsExample from docs
You can do this while creating the stack navigator as well. Updated as of react-navigation v4.
If you are on version 5, and are dealing with a functional component, you can use a layout effect hook to accomplish this (example from the docs reference):
Note that if you are changing
headerLeft
, you may need to pass a function to it (GH issue reference).