1
  1. 我避免了 registerSuccess 和 loginSuccess、registerFailure 和 loginFailure 的重复代码。
  2. 我分别需要不同的类型
    name: 'auth',
    initialState,
    reducers: {
        loginSuccess: (state, action) => {
            localStorage.setItem('token', action.payload.token);
            state.isAuthenticated = true;
            state.isLoading = false;
        },
        loginFailure: (state, action) => {
            localStorage.removeItem('token');
            state.isLoading = false;
            state.token = null;
            state.user = null;
            state.isAuthenticated = false;
            state.error = action.error;
        },
        registerSuccess: (state, action) => {
            localStorage.setItem('token', action.payload.token);
            state.isAuthenticated = true;
            state.isLoading = false;
        },
        registerFailure: (state, action) => {
            localStorage.removeItem('token');
            state.isLoading = false;
            state.token = null;
            state.user = null;
            state.isAuthenticated = false;
            state.error = action.error;
        }
    }
});```

4

0 回答 0