How can I give a custom navigation to the header back arrow in ReactNavigation? I want the back arrow in the header to navigate to the screen I define and not to the previous screen.
Thanks.
Overriding the back button 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): function ProfileScreen({ navigation, route }) { const [value, onChangeText] = React. useState(route. params.
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.
Hi who ever is using react-navigation 5.x you might not be able to make navigation using navigationOptions
. The option is been replaced with options
.
Say your in screen -1 and navigated to screen-2 and then to screen-3. By default using react-navigation you can navigate from screen-3 to screen-2. But if you want customised navigation i.e., Say from above example if you want to navigate from screen-3 to screen-1.
If you would like to retain the view of back button and only override the onPress method, you can import { HeaderBackButton } from '@react-navigation/stack'
and assign that component to the headerLeft
option.
Example:
<RootStack.Screen
name="dashboard"
component={Dashboard}
options={({navigation, route}) => ({
headerLeft: (props) => (
<HeaderBackButton
{...props}
onPress={() => navigation.navigate('Home')}
/>
),
})}
/>
In above example on click of back button in Dashboard screen takes you to Home screen.
Enjoy & Thanks -Sukshith
You could try 2 things:
a) use headerMode: 'none'
in your sub-StackRouters
instead of your root router (named RouterComponent). Ideally you shouldn't have to do anything more then and the headers of the sub-StackRouters
would be displayed in your root router's header. I think I remember something similarly worked a while back for me, but I haven't tested it in a while now and I think it's unlikely that it will still work like this but you can test nevertheless.
b) and this is what I'm currently using in a different situation. To manually include the back button:
import { HeaderBackButton } from '@react-navigation/stack';
const navigationOptions = ({ navigation }) => ({
headerLeft: <HeaderBackButton onPress={() => navigation.goBack(null)} />,
})
const RouterComponent = StackNavigator({
Tabs: {
screen: Tabs
},
Profile: {
screen: ProfileStack,
navigationOptions
}
},{
mode: 'modal',
headerMode: 'none',
});
If above solution doesn't work,
Try to add navigationOptions directly to the ProfileStack definition.
const ProfileStack = StackNavigator({
ProfileHome: {
screen: ProfileHome,
navigationOptions: ({navigation}) => ({ //don't forget parentheses around the object notation
title: 'Profile',
headerLeft: <HeaderBackButton onPress={() => navigation.goBack(null)} />
})
},
ProfileEdit: { screen: ProfileEdit }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With