【问题标题】:React Native, reactnavigation, how do I goBack() to the previous screen, not the first screen, using a Drawer?React Native,反应导航,如何使用抽屉返回()到上一个屏幕,而不是第一个屏幕?
【发布时间】:2021-10-23 18:05:24
【问题描述】:

这是我的问题的演示:https://snack.expo.dev/xtklvo6eM

我想从 ScreenOne -> ScreenTwo -> ScreenThree -> goBack() 转到 ScreenTwo....

但是在 ScreenThree 上调用 navigation.goBack() 会将我带回 ScreenOne,而不是 ScreenTwo。

谁能告诉我这是为什么?

import * as React from 'react';
import { Button, View } from 'react-native';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';

function ScreenOne({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Button
        onPress={() => navigation.navigate('ScreenTwo')}
        title="Go to screen two"
      />
    </View>
  );
}

function ScreenTwo({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Button onPress={() => navigation.navigate('ScreenThree')} title="Go to screen three" />
    </View>
  );
}

function ScreenThree({ navigation }) {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Button onPress={() => navigation.goBack()} title="Try to go back to screen two.." />
    </View>
  );
}

const Drawer = createDrawerNavigator();

export default function App() {
  return (
    <NavigationContainer>
      <Drawer.Navigator initialRouteName="ScreenOne">
        <Drawer.Screen name="ScreenOne" component={ScreenOne} />
        <Drawer.Screen name="ScreenTwo" component={ScreenTwo} />
        <Drawer.Screen name="ScreenThree" component={ScreenThree} />
      </Drawer.Navigator>
    </NavigationContainer>
  );
}

【问题讨论】:

    标签: reactjs react-native react-navigation react-navigation-drawer


    【解决方案1】:
    猜你喜欢
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2021-01-25
    • 2022-01-22
    • 2018-12-30
    相关资源
    最近更新 更多