开发者问题收集

React Navigation-未定义不是一个对象(评估“this.navigation.navigate”)

2020-01-13
1108

我正在按照本教程实现用于用户身份验证的切换导航器: 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 - 导航问题“未定义不是对象(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;

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');
   })
};
Auticcat
2020-01-13

问题出在 function 关键字上,它不绑定 this 关键字。最好将其替换为 ES6 箭头函数,该函数会隐式地将 this 绑定到内部作用域:

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

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

Gaurav Roy
2020-01-13