Custom back navigation for reactnavigation back button

前端 未结 2 1663
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 11:09

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 sc

2条回答
  •  不知归路
    2021-02-14 11:25

    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';
    
    const navigationOptions = ({ navigation }) => ({
        headerLeft:  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:  navigation.goBack(null)} />
          })
        },
        ProfileEdit: { screen: ProfileEdit }
      }
    

提交回复
热议问题