嗨我有这个代码用于启动会话,我需要在登录后保存用户在localStorage中的信息
'use strict';
const defaultState ={
permission: localStorage.getItem('email') ? localStorage.getItem('email') : null
}
export const signInReducer = (state=defaultState, action)=>{
switch (action.type){
case 'SIGNIN_REQUEST':
return state;
case 'SIGNIN_FAILURE':
return state;
case 'SIGNIN_SUCCES':
return Object.assign({}, state, {
permission: action.payload.email ? JSON.stringify(action.payload.email) : null
})
default:
return state;
}
}
感谢您的帮助
答案 0 :(得分:0)
您不应该在减速器中保存到localStorage
。这些应该是纯净的,没有副作用。
尝试在您的动作创建者中执行此操作:
const signInSucess = (user) => {
localStorage.setItem('permission', user)
//your code...
return { type: "SIGNIN_SUCCES", payload: user }
}