我可以使用redux存储中的getState方法为我的应用程序实现实用程序函数吗



我有一个正在工作的react redux应用程序,它提供登录功能,并根据用户的角色将用户重定向到各自的页面。为了检查它们的角色,我使用auth-reducer提供的jwt,并在登录组件本身中对其进行解码(我认为它具有可提取的逻辑(,并重定向它们。我想做这样的事情-->

//checkAuthAndRole.js -- utility
const {isAuthenticated, token} = store.getState();
function checkAdmin() {
if(!isAuthenticated) {
return <Redirect to='/login' />
} else //decode token here if role === admin redirect them to admin page
}
// export this utility and call it in respective component

我能做这个吗?

在实用程序中使用state不是一个正确的设计。'isAuthenticated'和'令牌'可以传递给实用程序,然后实用程序可以决定加载的页面

最新更新