简体   繁体   English

Redux存储在重新加载页面时发生更改

[英]Redux store changes when reload page

I am implementing login with two different type of users using react redux. 我正在使用react redux与两种不同类型的用户实现登录。 This is my login method: 这是我的登录方法:

export const login = (credentials) => (dispatch) =>
    api.user.login(credentials).then(user => {
      localStorage.userJWT = user.token;
      localStorage.userEmail = user.email;
      localStorage.userId = user.id;
      localStorage.surname = user.surname;
      localStorage.type = user.type;
      dispatch(userLoggedIn(user));
});
  1. For the first type of user, I return from backend: token, email, id, surname. 对于第一类用户,我从后端返回:令牌,电子邮件,ID,姓。
  2. For the second type of user, I return from backend: token, email, id, type. 对于第二种类型的用户,我从后端返回:令牌,电子邮件,ID,类型。

I do some secured routes which the second type of user cannot access. 我做了一些第二类用户无法访问的安全路由。 So if the variable surname is returned, I define the specific route for that user. 因此,如果返回了变量surname ,我将为该用户定义特定的路由。

If type variable is returned, it shows properly everything in links and in redux store as well. 如果返回type变量,它会正确显示链接和redux存储中的所有内容。 However, if I reload the page, then it automatically changes variable type into undefined surname . 但是,如果我重新加载页面,那么它将自动将变量type更改为undefined surname

This is where I am trying to save the redux state in store even if I reload the page. 即使重新加载页面,这也是我试图在存储中保存redux状态的地方。 const store = createStore( rootReducer, composeWithDevTools(applyMiddleware(thunk)) ); const store = createStore(rootReducer,composeWithDevTools(applyMiddleware(thunk)));

 if(localStorage.userJWT && localStorage.userEmail && localStorage.userId && localStorage.type){
   const user = { token: localStorage.userJWT, email: localStorage.userEmail, id: localStorage.userId, type: localStorage.type};
   store.dispatch(userLoggedIn(user));
 }
 if(localStorage.userJWT && localStorage.userEmail && localStorage.userId && localStorage.surname){
   const user = { token: localStorage.userJWT, email: localStorage.userEmail, id: localStorage.userId, surname: localStorage.surname};
   store.dispatch(userLoggedIn(user));
 }

Can you please suggest why it is not following my if statement. 您能否提出为什么它不遵循我的if陈述。 Thank you in advance. 先感谢您。

The Redux store/state is re-initialized on reload. Redux存储/状态在重新加载时会重新初始化。 You can keep your auth info in local storage, then pull that out to use as your initial state. 您可以将身份验证信息保存在本地存储中,然后将其拉出以用作初始状态。 That way, your Redux state will always have that auth information, even on reload. 这样,即使在重新加载时,您的Redux状态也将始终具有该身份验证信息。

function reducer (state = initState(), action) {
    return state
}

function initState () {
    return { 
        token: localStorage.userJWT, 
        email: localStorage.userEmail, 
        id: localStorage.userId, 
        surname: localStorage.surname
    }
}

Or, if you want to keep your entire Redux state on reload, you can try to implement some package that does that, like redux-persist . 或者,如果您想让整个Redux状态保持在重载状态,则可以尝试实现一些实现此目的的软件包,例如redux-persist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM