When developing applications with Redux and Redux Toolkit, you may encounter an error message: Redux Toolkit Error: 'reducer' is a required argument and must be a function or an object of functions that can be passed to combineReducers.
This error tells us that we have configured our Redux store incorrectly. Many developers make this mistake, especially while defining reducers.
So, in this explanation, you will understand how to resolve the "Redux Toolkit Error: 'reducer' is a required argument" in Redux Toolkit.
Let's jump straight into the problem. Below is the code shared by a developer who encountered this problem.
Problem code
import React from 'react';
import ReactDOM from 'react-dom/client';
import { Provider } from 'react-redux';
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './reducer/counterReducer';
import App from './App';
const store = configureStore(counterReducer);
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<Provider store={store}>
<App />
</Provider>
);
Solution
Make a small adjustment when you see the error indicating that a "reducer" is required in your store setup. Instead of passing your reducers directly, put them inside an object under the "reducer" name.
Here's how it looks:
import React from 'react';
import ReactDOM from 'react-dom/client';
import { Provider } from 'react-redux';
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './reducer/counterReducer';
import App from './App';
const store = configureStore({ reducer: counterReducer });
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<Provider store={store}>
<App />
</Provider>
);
When configuring the Redux store, pass the combined reducers or individual reducer functions within an object under the 'reducer' key in the configureStore function.
Replace:
const store = configureStore(counterReducer);
To:
const store = configureStore({ reducer: counterReducer });
Exploring Further
If you have multiple reducers, you can combine them into a single reducer using the combineReducers function like this:
import { configureStore } from '@reduxjs/toolkit';
import { combineReducers } from 'redux';
import counterReducer from './reducer/counterReducer';
import anotherReducer from './reducer/anotherReducer';
const rootReducer = combineReducers({
counter: counterReducer,
another: anotherReducer
});
const store = configureStore({ reducer: rootReducer });
In this code snippet, combineReducers helps combine different reducers into one big reducer called rootReducer. Each key in the rootReducer represents a different part of your app's state, and the value associated with it is the reducer function that controls that part. Separate reducers can be used to organize different parts of your app's data, making it easier to understand and manage.
Closing Remarks
To quickly fix the 'Redux Toolkit Error:'reducer' is a mandatory argument', make sure your reducers are correctly organized. Use the Redux Toolkit's features by grouping reducers within an object using the 'reducer' key. When dealing with many reducers, use the 'combineReducers' function to manage the state properly in React applications.
We hope you found this solution useful in your project. If you have any other questions or any error related to web development for which you did not find the solution, then you can ask in the question box given below and you will get the answer quickly.