我正在使用React Navigation实现身份验证。我希望如果有一个有效的令牌,该用户将被定向到应用程序导航器。否则,他将被定向到身份验证导航器。
在我的实现中,正在加载屏幕,同时是auth,但我什么都没看到。
index.js:
import { createSwitchNavigator, createAppContainer } from 'react-navigation';
import app from './app';
import auth from './auth';
import loading from './loading-screen'
export default createAppContainer(createSwitchNavigator(
{
loading,
app,
auth
}
));
loading-screen.js:
import React from 'react';
import {
ActivityIndicator,
AsyncStorage,
StatusBar,
View,
} from 'react-native';
export default class LoadingScreen extends React.Component {
constructor(props) {
super(props);
this.getUser();
}
getUser = async () => {
const token = await AsyncStorage.getItem('token');
this.props.navigation.navigate(token ? 'app' : 'auth');
};
render() {
return (
<View>
<ActivityIndicator />
<StatusBar barStyle="default" />
</View>
);
}
};
app.js:
import React from 'react';
import { createStackNavigator } from 'react-navigation';
import { View, Text } from 'react-native';
const login = () => (
<View>
<Text>login</Text>
</View>
);
export default createStackNavigator({login});
如果我从不具有createStackNavigator
功能的auth.js中导出登录组件,则它可以正常工作,并且我在屏幕上看到登录信息。有了它,就没有了。