Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying back button with react-navigation on specific screen

Here I'm using react-navigation as my navigation library.

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 the screen name (again only on one component).

like image 699
greW Avatar asked Mar 25 '18 15:03

greW


People also ask

How do you override the Back button in React Native?

To handle the Android Back Button Press in the React Native we have to register the hardwareBackPress event listener with a callback function, which will be called after pressing the Back Button.

How to go back (or forward) by one screen in React Native?

However, in React Native, there is a better way to allow users to go back (or forward) by one screen. You can implement the button that, when clicked, returns the user to a specific screen. In react-navigation, you can do that using the goBack () method. The goBack () method is one of the most important methods in the react-navigation library.

How do I go back to the previous screen in react-navigation?

In react-navigation, you can do that using the goBack () method. The goBack () method is one of the most important methods in the react-navigation library. It allows you to go back to one of the previous screens in your navigation stack.

How to go back to a specific screen in the navigation stack?

Clicking this button will take you back to the screen you viewed before the DetailsScreen component. It’s important to understand that to go back to a specific screen in the navigation stack using the goBack method, you must call it from the component you want to go back from.

How to override the back button in stacknavigator?

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:- Show activity on this post. You can override back button for a specific screen with navigationOptions of that screen.


1 Answers

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:-

import { HeaderBackButton } from 'react-navigation';
static navigationOptions = ({navigation}) => {
  return{
    headerLeft:(<HeaderBackButton onPress={()=>{navigation.navigate('A')}}/>)
 }
}
like image 94
Varun Gupta Avatar answered Sep 19 '22 17:09

Varun Gupta