React native 即使我关闭应用程序,如何使用Asyncstorage让用户登录react native?
如何让用户在不使用redux的情况下在react native中使用AsyncStorage登录?如果您有任何源代码,请提供它请按照以下说明将AsyncStorage与react本机官方文档一起使用React native 即使我关闭应用程序,如何使用Asyncstorage让用户登录react native?,react-native,React Native,如何让用户在不使用redux的情况下在react native中使用AsyncStorage登录?如果您有任何源代码,请提供它请按照以下说明将AsyncStorage与react本机官方文档一起使用首先将数据存储到本地存储器,如下代码所示 const login = await AsyncStorage.getItem('login'); if (login !== null) { // We have data!! // navigate to the ho
首先将数据存储到本地存储器,如下代码所示
const login = await AsyncStorage.getItem('login');
if (login !== null) {
// We have data!!
// navigate to the home or any other screen
}
else{
// navigate to the Login screen or any other screen
}
wait AsyncStorage.setItem('login',True')代码>
在关闭应用程序之后。每当用户再次打开应用程序时,请检查本地存储以进行登录,如下面的代码所示
const login = await AsyncStorage.getItem('login');
if (login !== null) {
// We have data!!
// navigate to the home or any other screen
}
else{
// navigate to the Login screen or any other screen
}
有关模式详细信息和存储详细信息,请检查此项
若你们仍然面临这个问题,请告诉我,若你们有什么想法,请在app.js中(Y)
const MainNavigator = createStackNavigator(
{
LoginPage: {screen : LoginPageContainer, navigationOptions: { header: null } },
MiddlePage: {screen : MiddlePageContainerIndex, navigationOptions: { header: null } }
}
);
在登录页面中
import { getItem } from '../../api/getItem';
componentDidMount = async() => {
let dataToken = await getItem();
//if token is available redirect to middlepage
if(dataToken !== null) {
const { navigate } = this.props.navigation;
navigate('MiddlePage');
}
}
在getItem.js中
import AsyncStorage from '@react-native-community/async-storage';
export const getItem = async() => {
try {
const value = await AsyncStorage.getItem('@storage_token');
if( value !== null ) {
return JSON.parse(value);
}else {
return null;
}
} catch (error) {
console.log(error);
return null;
}
}
希望能有帮助
另请阅读:。当您陷入困境时,需要Stackoverflow,而不仅仅是问您想要的问题。欢迎使用Stack Overflow!请参加本教程并通读[我如何提问?])(),然后发布一个您尝试的示例,并具体说明您的困境。请向我提供获取api和登录注销方法的完整代码