Как избежать дублирования редуктора в redux-toolkit

  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;
        }
    }
});```

0 ответов

Другие вопросы по тегам