在redux-toolkit中使用configurestore时出现“No reducer provided for key“auth””错误

sxissh06  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(257)

得到'No reducer provided for key“auth”'错误,认为这与我如何将其从切片传递到存储有关。除了“无减速器”错误外,一切正常。我应该使用rootReducer吗?我看了文件,但一无所获
The store.js

import { configureStore } from '@reduxjs/toolkit';
import alertReducer from '../features/alerts/alertSlice';
import authReducer from '../features/auth/authSlice';

export const store = configureStore({
  reducer: {
    alert: alertReducer,
    auth: authReducer
  },
});

authslice.js

import { createSlice } from '@reduxjs/toolkit';
import api from '../../utils/api';
import { setAlert } from '../alerts/alertSlice';

const slice = createSlice({
  name: 'auth',
  initialState: {
    token: localStorage.getItem('token'),
    isAuthenticated: null,
    loading: true,
    user: null,
  },
  reducers: {
    registerSuccess: (state, action) => {
      const { payload } = action.payload;
      state.push({
        payload,
        isAuthenticated: true,
        loading: false,
      });
    },
    registerFail: (state) => {
      localStorage.removeItem('token');
      state.token = null;
      state.isAuthenticated = false;
      state.loading = false;
      state.user = null;
    },
  },
});

const { registerSuccess, registerFail } = slice.actions;

export default slice.reducer;

// Register User
export const register = (formData) => async (dispatch) => {
  try {
    const res = await api.post('/users', formData);

    dispatch(registerSuccess(res.data));
  } catch (err) {
    const errors = err.response.data.errors;

    if (errors) {
      errors.forEach((error) => dispatch(setAlert(error.msg, 'danger')));
    }

    dispatch(registerFail());
  }
};

无减速器错误堆栈

No reducer provided for key "auth" vendors~main.chunk.js:36893:20
    e http://localhost:3000/static/js/vendors~main.chunk.js:36893
    warning http://localhost:3000/static/js/vendors~main.chunk.js:45798
    combineReducers http://localhost:3000/static/js/vendors~main.chunk.js:45882
    configureStore http://localhost:3000/static/js/vendors~main.chunk.js:1121
    js http://localhost:3000/static/js/main.chunk.js:320
    js http://localhost:3000/static/js/main.chunk.js:400
    __webpack_require__ http://localhost:3000/static/js/bundle.js:852
    fn http://localhost:3000/static/js/bundle.js:151
    js http://localhost:3000/static/js/main.chunk.js:1965
    js http://localhost:3000/static/js/main.chunk.js:2071
    __webpack_require__ http://localhost:3000/static/js/bundle.js:852
    fn http://localhost:3000/static/js/bundle.js:151
    js http://localhost:3000/static/js/main.chunk.js:1194
    js http://localhost:3000/static/js/main.chunk.js:1324
    __webpack_require__ http://localhost:3000/static/js/bundle.js:852
    fn http://localhost:3000/static/js/bundle.js:151
    js http://localhost:3000/static/js/main.chunk.js:895
    js http://localhost:3000/static/js/main.chunk.js:1177
    __webpack_require__ http://localhost:3000/static/js/bundle.js:852
    fn http://localhost:3000/static/js/bundle.js:151
    js http://localhost:3000/static/js/main.chunk.js:137
    js http://localhost:3000/static/js/main.chunk.js:297
    __webpack_require__ http://localhost:3000/static/js/bundle.js:852
    fn http://localhost:3000/static/js/bundle.js:151
    js http://localhost:3000/static/js/main.chunk.js:1843
    js http://localhost:3000/static/js/main.chunk.js:1950
    __webpack_require__ http://localhost:3000/static/js/bundle.js:852
    fn http://localhost:3000/static/js/bundle.js:151
    1 http://localhost:3000/static/js/main.chunk.js:2085
    __webpack_require__ http://localhost:3000/static/js/bundle.js:852
    checkDeferredModules http://localhost:3000/static/js/bundle.js:46
    webpackJsonpCallback http://localhost:3000/static/js/bundle.js:33
    <anonymous> http://localhost:3000/static/js/main.chunk.js:1
cu6pst1q

cu6pst1q1#

前端中未导入{registerSuccess,registerFail}的愚蠢错误

相关问题