javascript - 如果用户被 redux 商店“未经身份验证”,则呈现登录页面
问题描述
我有一个使用 redux 来发送身份验证信息的应用程序设置,但我不确定如何根据用户是否“加载”/身份验证来正确呈现内容。
应用组件(应用的入口点是一个基本的 React index.js):
class App extends Component {
componentDidMount() {
store.dispatch(loadUser())
}
render() {
return (
<Provider store={store}>
<div className="App">
{/* If there is a 401 error or the user isn't authenticated render the login page */}
{/* If the user is authenticated and there is a token in the store render content */}
</div>
</Provider>
);
}
}
加载用户函数:
export const loadUser = () => (dispatch, getState) => {
// User loading
dispatch({
type: USER_LOADING
})
axios.get('/users/user', tokenConfig(getState))
.then(res => {
dispatch({
type: USER_LOADED,
payload: res.data
})}
)
.catch(err => {
// console.log(err)
dispatch(returnErrors(err.response.data, err.response.status))
dispatch({
type: AUTH_ERROR
})
})
}
我想实现我在 App 评论中提出的逻辑。
根据我对 redux 的了解,组件总是会先渲染,然后在 redux 的商店更新时,组件将被更新/重新渲染。我如何实现这一目标?
我尝试过使用 componentDidUpdate、getDerivedStateFromProps 和其他生命周期方法,但我无法弄清楚
解决方案
推荐阅读
- java - 我应该使用哪种图形算法来找到通过所有给定目的地的最快路线?
- c - 了解 SISL 库中的原型/函数语法
- python-3.x - 你如何让 pytest 像 python 一样理解 python 模块?
- javascript - 在 vuejs 中,输入事件触发在列表中不起作用
- azure-cosmosdb - 如何使用 pymongo 获取 CosmosDB 查询中消耗的 RU/s?
- python - 绘制 3D 曲面图,其中颜色条代表另一个数量
- homebrew - 将 OSX 升级到 Catalina 后出现 zsh 问题
- excel - Excel 将存储为文本 YYYYMMDDhhmmss 的 DateTime 转换为格式化的日期和时间“mm/dd/yy hh:mm”
- sql - 表未按预期加入
- heroku - 由于某种原因,heroku 继续随机停止