【问题标题】:how to handle back button function in AppStack version 4.0 ( headerLeft )?如何处理 AppStack 4.0 版( headerLeft )中的后退按钮功能?
【发布时间】:2021-02-08 09:31:15
【问题描述】:
  • 在页面中应用 js
  • 反应:钩子 导航:第 4 版
  • backLeft 错误 ReferenceError:找不到变量:导航
  • 我想回到上一个屏幕,但是 back() 函数不起作用。
  • 导航和返回功能无法识别。
import Info from "./Screens/Info";
import Note from "./Screens/Note";
import { createAppContainer } from "react-navigation";
import { createStackNavigator } from "react-navigation-stack";
function App() {
  return (
    <SafeAreaView style={styles.container}>
      <StatusBar barStyle="dark" />
      <AppContainer />
      <View>
      </View>
    </SafeAreaView>
  );
}

const AppStack = createStackNavigator(
  {
    Start: {
      screen: Start,
      navigationOptions: {
        title: '',
      },
      params: { user: 'me' },
    },

    Info: {
      screen: Info,
      navigationOptions: { title: 'الصفحة الرئيسية' },
    },
    Guide: {
      screen: Guide,
      navigationOptions: { title: 'دليل الصيانة' },
    },
    Note: {
      screen: Note,
      navigationOptions: {
        title: 'المذكرة',
// **the error is here ⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇❌❌❌❌❌❌**
        headerLeft: () => <TouchableOpacity onPress={() => { goBack() }} ><Text style={{ color: '#fff' }}>back</Text></TouchableOpacity>
      },
    },

  },
  {
    initialRouteName: "Start",
  
  
   
    },

  }
);

export default createAppContainer(AppStack);

【问题讨论】:

    标签: reactjs react-native navigation react-hooks


    【解决方案1】:

    试试这个:

    Note: {
      screen: Note,
      navigationOptions: ({ navigation }) => ({
        title: 'المذكرة',
        headerLeft: () => <TouchableOpacity onPress={() => { navigation.goBack() }} ><Text style={{ color: '#fff' }}>back</Text></TouchableOpacity>
      }),
    },
    

    您可以像上面一样访问navigationOptions中的navigation对象,然后它将用于导航路线。

    【讨论】:

    • // 返回并用替换 navigation.replace('Info') 重新渲染
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 2012-01-27
    • 2011-03-04
    • 2012-01-24
    • 2015-08-29
    相关资源
    最近更新 更多