我正在使用本机响应,现在尝试安装加载组件时出现以下错误。
TypeError:未定义不是对象(正在评估'this.props.navigation.navigate')
这是我的代码
import React from 'react';
import SplashScreen from 'react-native-splash-screen';
import Loading from '../../components/loading/Loading';
import { connect } from 'react-redux';
class LoadingContainer extends React.Component {
componentDidMount() {
setTimeout(() => {
SplashScreen.hide();
}, 300);
let initRoute;
if (this.props.auth.user) {
initRoute = 'App';
} else {
initRoute = 'Login';
}
this.props.navigation.navigate(initRoute);
}
render() {
return <Loading navigation={this.props.navigation} />;
}
}
const mapStateToProps = state => ({
auth: state.auth,
});
export default connect(mapStateToProps, null)(LoadingContainer);
这是我在使用react-navigation时第一次遇到此错误,我使用的是2.11.2版本的react-navigation
答案 0 :(得分:0)
为什么不使用react-navigation v4.x?
对于v2.x,您的导航配置可能如下所示
import React from 'react';
import { createStackNavigator } from 'react-navigation';
import LoadingContainer from '../containers/LoadingContainer/LoadingContainer';
import Login from '../containers/LoginContainer/Login';
const AppStack = createStackNavigator(
{
Loading: LoadingContainer,
Login: Login,
},
{
initialRouteName: 'Loading',
}
);
export default class App extends React.Component {
render() {
return <AppStack />
}
}