Home > Software design >  Expected to find one of the known reducer keys instead: "user". Unexpected keys will be ig
Expected to find one of the known reducer keys instead: "user". Unexpected keys will be ig

Time:09-23

I am trying to use Redux to update a users profile. I keep getting the error

Expected to find one of the known reducer keys instead: "user". Unexpected keys will be ignored.

I have my store created with this:

import {configureStore} from '@reduxjs/toolkit';
import userProfileReducer from './slices/user';

const store = configureStore({
  reducer: {
    user: userProfileReducer,
  },
});

export default store;

The userSlice is below which is getting the error. I am passing the initial state and then using that to send to the reducer:

const initialUserState = {
  user: {
    email: '',
    name: '',
    phoneNumber: '',
    isAdmin: false,
  },
};

const UserSlice = createSlice({
  name: 'user',
  initialState: initialUserState,
  reducers: {
    user(state, action) {
      state.email = action.payload.email;
      state.name = action.payload.name;
      state.isAdmin = action.payload.isAdmin;
      state.phoneNumber = action.payload.phoneNumber;
    },
  },
});

export const userActions = UserSlice.actions;
export default UserSlice.reducer;

CodePudding user response:

The first problem that I can spot is that you have one level of too deep nesting. It should be:

const initialUserState = {
  email: '',
  name: '',
  phoneNumber: '',
  isAdmin: false,
};

Does this solve the above problem?

  • Related