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
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.or
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:
Notice that I’ve changed the
reducer
field from a function to an object with thepostsReducer
assigned to theposts
key. This should resolve the issue you’re facing with the Redux DevTools showingreducer: undefined
.With this change, your Redux store configuration should work correctly and your reducers should be properly connected to the store.