This error occurs when using Redux Toolkit's createSlice function without providing a valid reducers object. The createSlice API requires a reducers configuration object to generate action creators and reducer logic automatically.
Fixes You must pass in a reducers object as the first argument to createSlice
import { createSlice } from '@reduxjs/toolkit';
const mySlice = createSlice({
name: 'sliceName',
initialState: { /* initial state */ },
reducers: {
// Your case reducer functions here
increment: (state) => {
state.value += 1;
},
},
});import { createSlice } from '@reduxjs/toolkit';const mySlice = createSlice({name: 'sliceName',initialState: { /* initial state */ },reducers: {// Your case reducer functions hereincrement: (state) => {state.value += 1;},},});You must pass in a reducers object as the first argument to createSlice
The createSlice function from Redux Toolkit is a utility that helps simplify Redux reducer and action creation. It expects a configuration object with a reducers field containing case reducer functions. When you call createSlice without passing a reducers object, or when the reducers argument is undefined or null, Redux Toolkit throws this error to indicate that the required configuration is missing.
Ensure you are passing an object with a reducers property to createSlice. The basic structure should look like:
import { createSlice } from '@reduxjs/toolkit';
const mySlice = createSlice({
name: 'sliceName',
initialState: { /* initial state */ },
reducers: {
// Your case reducer functions here
increment: (state) => {
state.value += 1;
},
},
});If you intentionally want a slice with no reducers (uncommon), you still need to provide an empty object:
const mySlice = createSlice({
name: 'sliceName',
initialState: {},
reducers: {}, // Empty but present
});However, typically you'll want to define at least one reducer function.
Make sure you are importing createSlice correctly:
// Correct import for Redux Toolkit
import { createSlice } from '@reduxjs/toolkit';
// Not from 'redux' or other packagesAlso ensure your Redux Toolkit version is compatible with your usage. Check package.json for @reduxjs/toolkit version.
If you are creating the configuration object dynamically, ensure the reducers property is correctly assigned:
// Example of dynamic creation (still needs reducers)
const sliceConfig = {
name: 'mySlice',
initialState,
reducers: myReducers, // Make sure myReducers is defined and not undefined
};
const mySlice = createSlice(sliceConfig);Create a minimal slice to isolate the issue:
import { createSlice } from '@reduxjs/toolkit';
const testSlice = createSlice({
name: 'test',
initialState: { value: 0 },
reducers: {
testAction: (state) => {
state.value += 1;
},
},
});
console.log(testSlice.actions.testAction());If this works, compare with your original code to find differences.
The createSlice function internally uses createAction and createReducer. The reducers object is mandatory because it defines the action types and the corresponding state updates. If you need to respond to actions defined outside the slice (like thunks or other slices), use the extraReducers field instead. Note that extraReducers is optional and doesn't generate action creators, while reducers is required for generating slice-specific actions.