【问题标题】:navigation.push is not a function. (In 'navigation.push("EditProfile")', 'navigation.push' is undefined) (React navigation V6)navigation.push 不是函数。 (在\'navigation.push(\"EditProfile\")\'中,\'navigation.push\'是未定义的)(React navigation V6)
【发布时间】:2022-11-11 09:19:37
【问题描述】:

我是 React Native 的新手,我正在尝试执行简单的堆栈导航。我让它在应用程序的另一部分工作(用户身份验证步骤)。一旦用户登录,我的代码就会进入另一个堆栈。这个堆栈导航器有一个嵌套的选项卡导航器,可能会导致问题?

无论哪种方式,我都无法执行从我的个人资料屏幕到编辑个人资料屏幕的推送。代码如下。

import React from 'react'
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import LoginScreen from './screens/LoginScreen';
import HomeScreen from './screens/HomeScreen';
import SignupScreen from './screens/SignupScreen';
import ProfileScreen from './screens/ProfileScreen';
import EditProfileScreen from './screens/EditProfileScreen';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import Icon from 'react-native-vector-icons/Ionicons';

const Stack = createNativeStackNavigator()

const screenOptions = {

headerShown: false

}

export const SignedOutStack = () =\> (

\<NavigationContainer\>

\<Stack.Navigator

initialRouteName="LoginScreen"

screenOptions={screenOptions}

\\>

\<Stack.Screen

name="Login"

component={LoginScreen}

/\>

\<Stack.Screen

name='SignupScreen'

component={SignupScreen}

/\>

\</Stack.Navigator\>

\</NavigationContainer\>

)

const Tab = createBottomTabNavigator();

export const SignedInStack = () =\> (

\<NavigationContainer\>

\<Tab.Navigator

screenOptions={screenOptions}\>

\<Tab.Screen name="Home" component={HomeScreen} /\>

\<Tab.Screen name="Profile" component={ProfileScreen} /\>

\</Tab.Navigator\>

\</NavigationContainer\>

)

export const ProfileStack = () =\> (

\<NavigationContainer\>

\<Stack.Navigator\>

\<Stack.Screen name="SignedInStack" component={SignedInStack} /\>

\<Stack.Screen name="EditProfile" component={EditProfileScreen} /\>

\</Stack.Navigator\>

\</NavigationContainer\>

)

我正在尝试实施推送的地方

import { View, Text, SafeAreaView, ScrollView, StyleSheet, Image, TouchableOpacity, StatusBar, Button } from 'react-native'
import React, {useState, useEffect, useContext} from 'react';
import EditProfileScreen from './EditProfileScreen';

const ProfileScreen = ({navigation}) =\> (

    // const {user, logout} = useContext(AuthContext)
    
    <SafeAreaView style={styles.wrapper}>
      <ScrollView 
        style={styles.container}
        contentContainerStyle={{justifyContent: 'center', alignItems: 'center'}}
        showsVerticalScrollIndicator = {false}
        >
            <Image style={styles.userImg} source={{uri: 'https://www.dmarge.com/wp-content/uploads/2021/01/dwayne-the-rock-.jpg'}} />
            <Text style={styles.userName}>Person</Text>
            <View style={styles.userBtnWrapper}>
        
                <Button title="Edit Profile" onPress = {() => {
                    navigation.push("EditProfile")
                    }}/>
    
            </View>
      </ScrollView>
    </SafeAreaView>

)

export default ProfileScreen

【问题讨论】:

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


    【解决方案1】:

    在实际使用导航之前,您可能需要声明一个 stackAction。

    import { StackActions } from '@react-navigation/native';
    

    ……

    const pushAction = StackActions.push('Profile', { user: 'Wojtek' });
    
    navigation.dispatch(pushAction);
    

    【讨论】:

    • 我有太多导航容器,而且它们的嵌套方式错误。不过谢谢!
    猜你喜欢
    • 2018-12-07
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2019-09-08
    • 2022-01-02
    相关资源
    最近更新 更多