【问题标题】:React Navigation - undefined is not an object (evaluating 'this.navigation.navigate')React Navigation - undefined 不是一个对象(评估'this.navigation.navigate')
【发布时间】:2020-04-30 01:07:17
【问题描述】:

我正在按照本教程实现用于用户身份验证的切换导航器:https://snack.expo.io/@react-navigation/auth-flow-v3

但是,当我尝试导航到下一个屏幕时,this.navigation.navigate 似乎未定义。

undefined is not an object (evaluating 'this.props.navigation.navigate')

我正在为我的应用程序使用 expo,并且我已经查看了在 React Native - navigation issue "undefined is not an object (this.props.navigation.navigate)" 发布的类似问题的解决方案,但无济于事。

import * as React from 'react';
import { createBottomTabNavigator } from 'react-navigation-tabs';

import profile from './app/screens/profile.js'
import home from './app/screens/home.js'
import createCompetition from './app/screens/create.js'
import explore from './app/screens/explore.js'
import Icon from 'react-native-vector-icons/MaterialIcons'
import login from './app/screens/login.js';
import { f } from './config/config.js';
import { ActivityIndicator, AsyncStorage, Button, StatusBar, StyleSheet, View } from 'react-native';
import { createStackNavigator } from 'react-navigation-stack';
import { createAppContainer, createSwitchNavigator } from 'react-navigation';
/** 
 * Tab Stack is the Bottom Navigator for the different pages
*/
const TabStack = createBottomTabNavigator(
  {
    Home: {
      screen: home,
      navigationOptions: {
        tabBarIcon: ({ tintColor }) => (
          <Icon name="home" size={25} style={{ color: tintColor }} />
        ),

      },
    },
    Explore: {
      screen: explore,
      navigationOptions: {
        tabBarIcon: ({ tintColor }) => (
          <Icon name="search" size={25} style={{ color: tintColor }} />
        ),

      }
    },
    Profile: {
      screen: profile,
      navigationOptions: {
        tabBarIcon: ({ tintColor }) => (
          <Icon name="person" size={25} style={{ color: tintColor }} />
        ),

      }
    },
    Create: {
      screen: createCompetition,
      navigationOptions: {
        tabBarIcon: ({ tintColor }) => (
          <Icon name="add" size={25} style={{ color: tintColor }} />
        ),

      }
    },
  },
  {
    tabBarOptions: {
      showIcon: true,
      showLabel: false,
      activeTintColor: 'black',

      style: { backgroundColor: 'white', }
    },

  },
)

/**
 * Loading Screen during authorization process
 */
class AuthLoadingScreen extends React.Component {
  constructor() {
    super();
    this._bootstrapAsync();
  }

  // Fetch the token from storage then navigate to our appropriate place
  _bootstrapAsync = async () => {
    f.auth().onAuthStateChanged(function (user) { //checks if user is signed in or out
      this.props.navigation.navigate(user ? 'App' : 'Auth');
    })
  };

  // Render any loading content that you like here
  render() {
    return (
      <View style={styles.container}>
        <ActivityIndicator />
        <StatusBar barStyle="default" />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
});

const AppStack = createStackNavigator({ Home: TabStack });
const AuthStack = createStackNavigator({ Login: login });
const RootStack = createSwitchNavigator(
  {
    AuthLoading: AuthLoadingScreen,
    App: AppStack,
    Auth: AuthStack,
  },
  {
    initialRouteName: 'AuthLoading',
  }
);

const App = createAppContainer(RootStack);
export default App;

【问题讨论】:

    标签: react-native react-native-navigation


    【解决方案1】:

    问题在于 function 关键字,它没有绑定 this 关键字。更好地将其替换为 ES6 箭头函数,该函数将 this 隐式绑定到内部范围:

    f.auth().onAuthStateChanged((user) => { //checks if user is signed in or out
       this.props.navigation.navigate(user ? 'App' : 'Auth');
       })
    

    希望对您有所帮助。如有疑问,请随意

    【讨论】:

      【解决方案2】:

      您没有向您的_bootstrapAsync 函数和您的onAuthStateChanged 回调授予对this 的访问权限。只需使用箭头函数在其中传递回调,因为它将当前函数自动绑定到当前应用程序this

      _bootstrapAsync = async () => {
         f.auth().onAuthStateChanged((user) => { //checks if user is signed in or out
         this.props.navigation.navigate(user ? 'App' : 'Auth');
         })
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-04
        • 2018-03-28
        相关资源
        最近更新 更多