skip to Main Content

When I check state in redux dev tools, I see reducers: undefined. I don’t know what is wrong with this code.

postsSlice.js

import { createSlice } from '@reduxjs/toolkit'

const postsSlice = createSlice({
  initialState: [{ id: '1', title: 'test', desc: 'test2' }],
  name: 'posts',
  reducers: {},
})

export default postsSlice.reducer

store.js

import { configureStore } from '@reduxjs/toolkit'
import postsReducer from '../features/postsSlice'


export default configureStore({
  reducer: () => ({
    posts: postsReducer,
  }),
})

redux dev tool

reducer(pin): undefined

changed repo, node modules

2

Answers


  1. The reducer property of the store creation takes an object with key-values that are reducer functions, or it takes a reducer function directly. It doesn’t take a function that returns this object/reducer function.

    import { configureStore } from '@reduxjs/toolkit';
    import postsReducer from '../features/postsSlice';
    
    export default configureStore({
      reducer: {
        posts: postsReducer,
      },
    });
    

    or

    import { configureStore } from '@reduxjs/toolkit';
    import postsReducer from '../features/postsSlice';
    
    export default configureStore({
      reducer: postsReducer,
    });
    
    Login or Signup to reply.
  2. There might be an issue with how you’re configuring the Redux store. In your store.js, you’re using a function for the reducer field, but it should be an object.

    Here’s how you can fix the store.js code:

    import { configureStore } from '@reduxjs/toolkit';
    import postsReducer from '../features/postsSlice';
    
    export default configureStore({
      reducer: {
        posts: postsReducer,
      },
    });
    

    Notice that I’ve changed the reducer field from a function to an object with the postsReducer assigned to the posts key. This should resolve the issue you’re facing with the Redux DevTools showing reducer: undefined.

    With this change, your Redux store configuration should work correctly and your reducers should be properly connected to the store.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search