在执行某些操作后,Redux自动返回initalState

时间:2019-05-21 19:00:22

标签: react-native redux react-redux redux-thunk feathersjs

我正在使用react-redux,并且在调用某个操作时,reducer自动返回初始状态,例如,当我调用fetchPost操作或createPost操作时,(给定的代码)我的Auth reducer自动返回初始状态。 为什么会发生这种情况,所以我正在使用redux-thunk和带有feathersJS的后端。


export const login = (payload) => dispatch => {
    AsyncStorage.getItem('feathers-jwt').then(r => {
        dispatch({ type: 'CHECK_AUTHORIZATION', payload: r })
    }).catch(() => {
        client.authenticate({
            strategy: 'local',
            email: payload.email,
            password: payload.password
        }).then(r => {;
            dispatch({ type: 'LOGIN_REQUEST', payload: r })
        }).catch(e => {
            dispatch({ type: 'LOGIN_ERROR', payload: e.message })
        })
    })
}

export const checkAuthorization = () => dispatch => {
    AsyncStorage.getItem('feathers-jwt').then(r => {
        dispatch({ type: 'CHECK_AUTHORIZATION', payload: r });
     return client.passport.verifyJWT(r);
        }).then(payload => {
            dispatch({ type: 'JWT_PAYLOAD', payload: payload })
            return client.service('users').get(payload.userId);
        }).then(user => {
            client.set('user', user);
            client.get('user').then(user => {
                dispatch({ type: 'PROFILE', payload: user })
            })
        })
    .catch(() => {
        dispatch({ type: 'AUTHORIZATION_FAILED' })
    })
}

export const logout = (payload) => dispatch => {
    client.logout().then(r => dispatch({ type: 'LOG_OUT', payload: r }))
}

import actions from './actions';
import AsyncStorage from '@react-native-community/async-storage';

const initalState = {
  users: [],
  isAuthenticated: false,
  accessToken: null,
  profile: null,
  isVendor: false,
  isConsumer: false,
  errorMessage: null,
  jwtPayload: null
};

export default (state = initalState, action) => {
    switch (action.type) {
        case 'CHECK_AUTHORIZATION':
            return Object.assign({}, state, {
                accessToken: action.payload,
                isAuthenticated: true
            })
        case 'AUTHORIZATION_FAILED':
            return Object.assign({}, state, {
                isAuthenticated: initalState.isAuthenticated
            })
        case 'LOGIN_REQUEST':
            return (
                Object.assign({}, state, {
            accessToken: action.payload.accessToken,
            isAuthenticated: true,
        }));
        case 'JWT_PAYLOAD':
            return Object.assign({}, state, {
                jwtPayload: action.payload
            })
        case 'PROFILE':
            return Object.assign({}, state, {
                profile: action.payload
            })
        case 'LOGIN_ERROR':
            return Object.assign({}, state, {
                errorMessage: action.payload.message
            })
        case 'LOG_OUT': 
            return Object.assign({}, state, {
                isAuthenticated: initalState.isAuthenticated,
                accessToken: null
            })
        default:
            return initalState;
        }
}

2 个答案:

答案 0 :(得分:0)

尝试将默认设置设置为

默认值:       返回状态; <​​/ p>

而不是 默认:             返回initalState;

答案 1 :(得分:0)

有时,我们可能会在提交表单后提出要求。然后重新加载页面,并从头开始初始化整个redux。

例如

<form>
  <button type="submit" onClick={()=>dispatch(someAction)}>Foo</button>
</form>

以上代码将调用该操作,redux将再次初始化。