【发布时间】:2020-12-20 03:40:26
【问题描述】:
在我的项目中,当uid保存在AsyncStorage中时,表示用户已经登录。
当前代码
索引.js
import React from 'react';
import Auth from 'app/src/common/Auth';
import { Text } from 'react-native';
export default class Index extends React.Component {
constructor(props) {
super(props);
this.state = {
auth: true,
};
}
async componentDidMount() {
await this.setState({ auth: await Auth.user() });
}
render() {
const { auth } = this.state;
return (
{!auth ? (
<Text>You need to Log In!</Text>
) : (
<Text>You are Logged In!</Text>
)}
)
}
}
Auth.js
import { AsyncStorage } from 'react-native';
export default {
async user() {
let result = true;
const response = await AsyncStorage.getItem('uid');
if (!response) {
result = false;
}
return result;
},
};
这段代码可以运行,但我想让它更简单,就像一个函数一样。
如果您能给我任何建议,我将不胜感激。
【问题讨论】:
标签: reactjs react-native jwt