反应导航,返回嵌套导航

时间:2020-04-19 12:28:44

标签: react-native react-navigation react-navigation-v5

Stack Navigator :
 - Login
 - Register
 - Activation
 - Tab Navigator
    + Home
    + Histories
    + Cart
    + Profile
 - Outlet
 - Product

我在返回导航时遇到问题,当我进入产品屏幕时,可以使用navigation.navigate('Cart');转到“购物车屏幕”,但是在“购物车屏幕”和goBack()中,我的屏幕转到“主屏幕”。 如何从堆栈屏幕到标签屏幕管理goBack()

1 个答案:

答案 0 :(得分:0)

在react-navigation(在5之前)中,我确实做到了这一点,您可以通过使用这样的自定义导航组件将上层导航作为向下传递的道具:



// this component's navigation is replaced by upper navigation, you can send another prop if you need both navigations.
const MyComponent0 = createStackNavigator(
  {
    Child1: {
      screen: Child1,
      navigationOptions: ({navigation}) => {
        return {
          headerRight: (
            // you can use BackButton of library with import it, I wrote my own, that time i didn't know there is a ready importable back button.
            <TouchableOpacity
              style={{paddingLeft: 10}}
              onPress={() => navigation.toggleDrawer()}>
              <Icon ios="ios-menu" android="md-menu" style={{color: '#fff'}} />
            </TouchableOpacity>
          ),
          headerTitle: <Text style={styles.whiteText}>Child1 title</Text>,
          headerLeft: (
            <HeaderBackButton
              style={{color: '#FFF'}}
              onPress={() => navigation.goBack(null)}
            />
          ),
        };
      },
    },
    Child2,
    ...
  },
  {
    defaultNavigationOptions: {
      headerTitleStyle: {
        color: '#fff',
        fontSize: 13,
        ...Platform.select({
          android: {
            fontFamily: ...,
          },
          ios: {
            fontFamily: ...,
          },
        }),
      },
      headerStyle: {
        backgroundColor: 'rgba(0,0,0,1)',
      },
      headerTintColor: 'white',
    },
  },
);


class MyComponent1 extends Component {
  constructor() {
    super();
  }
  static router = MyComponent0.router;

  render() {
    return <MyComponent0 navigation={this.props.navigation} />; // you can send like navigation1 if you need downward navigation too.
  }
}

此代码采用的是旧方法,但可以在5之前使用react-navigation使用。 希望对您有帮助。