reactjs - authProvider is finishing its Promise after the Dashboard being mounted
问题描述
解决方案
Try using async/await
, it's bit easier to follow the flow:
export default async (type, userID) => {
if (type === AUTH_LOGIN) {
try {
const res = await fetch('https://localhost:8080/users/auth', {
method: 'POST',
body: JSON.stringify({ userID })
headers: new Headers({ 'Content-Type': 'application/json' }),
}) // fetch API data
const { user: { token, name, email, activePage } } = await res.json(); // convert to JSON, from the JSON's user prop, pull out token, name, email, activePage
localStorage.setItem('token', token);
localStorage.setItem('name', name);
localStorage.setItem('email', email);
localStorage.setItem('activePage', activePage);
} catch(err) { console.error(err); }
}
}
推荐阅读
- c++ - GCC 如何在堆栈上创建一个数组而不用常量变量给出它的大小?
- linux - 当 cr3 设置为进程的目录库时,MMU 如何转换内核虚拟地址?
- pytest - 将测试嵌入与实现相同的模块中
- web3 - ModuleNotFoundError:运行 web3 python 代码时没有名为“lru”的模块
- filter - 我应该使用哪个应用程序来分析布隆过滤器
- python - 输出扩展节点有一些问题。我该如何解决?
- excel - 计算没有辅助列/表的平均投资价格
- algorithm - 模糊数的重新解释
- architecture - 如何在 DDD 中实现仅查询子
- python - 我可以使用 Facebook Graph API 安排帖子吗