redux中的localStorage reducer错误

时间:2016-12-23 13:25:02

标签: javascript reactjs local-storage redux

嗨我有这个代码用于启动会话,我需要在登录后保存用户在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;
        }
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您不应该在减速器中保存到localStorage。这些应该是纯净的,没有副作用。

尝试在您的动作创建者中执行此操作:

const signInSucess = (user) => {
  localStorage.setItem('permission', user)
  //your code...
  return { type: "SIGNIN_SUCCES", payload: user }
}