React-native后退按钮转到第一页

时间:2019-02-14 22:50:58

标签: react-native react-native-android

我有一个从“测试”页面到“ TestDetails”页面的链接。但是,当我单击“ TestDetails”页面上的this.props.navigation.goBack(null)时,它将带我到第一页,即“主页”而不是“测试”。这是我的index.js。我真的不知道这是怎么实现的。请问我在做什么错

const HomeScreen = createStackNavigator({
  Home: {
    screen: Home,

    navigationOptions: ({navigation}) => ({

    })
  }
});
const LoginScreen = createStackNavigator({
  Home: {
    screen: Login,

     navigationOptions: ({navigation}) => ({

    })
  }
});

const SignupScreen = createStackNavigator({
  Signup: {
    screen: Signup,

    navigationOptions: ({navigation}) => ({

    })
  }
});

const TestsScreen = createStackNavigator({
  Tests: {
    screen: Tests,
    navigationOptions: ({navigation}) => ({
      headerTitleStyle: {
        fontWeight: '400',
        color: '#333',
        fontFamily: 'Nunito-Regular',
        fontSize: 18
      },
      headerStyle: {
        elevation: 0, //remove shadow on Android
        shadowOpacity: 0, //remove shadow on iOS
        fontWeight: 400,
      },
      title: "Consumer tests",
      headerLeft:(<Icon.Button name="menu" size={35} 
backgroundColor="transparent" color="#2a486c" onPress={() => 
navigation.dispatch(DrawerActions.toggleDrawer())}>

              </Icon.Button>      
      ),
    })
  }
});

const TestDetailsScreen = createStackNavigator({
  TestDetails: {
    screen: TestDetails,
    navigationOptions: ({navigation}) => ({

    })
  }
});

const SettingsScreen = createStackNavigator({
  Settings: {
    screen: Settings,
    navigationOptions: ({navigation}) => ({
      title: "Settings",
      headerLeft:(<Icon.Button name="arrow-back" backgroundColor="transparent" color="black" onPress={() => navigation.dismiss()}>
                  <Text style={{fontSize: 15}}></Text>
              </Icon.Button>      
      ),
    })     
  },
});


const drawernav = createDrawerNavigator({
  Home: {
    screen: HomeScreen,  
  },
  Login: {
    screen: LoginScreen,  
  },
  SignUp: {
    screen: SignupScreen,  
  },
  Tests: {
    screen: TestsScreen,  
  },
  TestDetails: {
    screen: TestDetailsScreen,  
  },
  Settings: {
    screen: SettingsScreen,  
  },
},{
    contentComponent: SideMenu,
});

AppRegistry.registerComponent(appName, () => createAppContainer(drawernav));

请问我在做什么错

2 个答案:

答案 0 :(得分:0)

const LoginScreen = createStackNavigator({

    Home: {

        screen: Login,

        navigationOptions: ({navigation}) => ({

        })
    }
})  

为什么回家:在这里?

您可以从控制台添加信息还是没有收到错误或警告? 切记始终参考官方指南https://facebook.github.io/react-native/docs/navigation

答案 1 :(得分:0)

我已经确认了react-native-navigation的定义,如下面的代码片段所示。 goBack()goBack(null)一样!

回到您的问题: 像@Michael帖子一样,您最好检查一下导航堆栈的拳头

  export interface NavigationScreenProp<S, P = NavigationParams> {
    state: S;
    dispatch: NavigationDispatch;
    goBack: (routeKey?: string | null) => boolean;
    dismiss: () => boolean;

    navigate(options: {
      routeName:
        | string
        | {
            routeName: string;
            params?: NavigationParams;
            action?: NavigationNavigateAction;
            key?: string;
          };
      params?: NavigationParams;
      action?: NavigationAction;
      key?: string;
    }): boolean;