Manage Redux State with createSlice and configureStore
Use Redux Toolkit to write reducers and configure a store with less boilerplate and built-in DevTools support.
21 Jul 2025, 20:24 UTC

The problem: hand-written Redux is verbose and easy to break
Plain Redux requires you to declare action type constants, write action creator functions, and manage switch statements in reducers. It is also easy to mutate state directly inside a reducer, which breaks Redux's immutability rule and invalidates time-travel debugging.
Takeaway
Use Redux Toolkit's createSlice to define actions and reducers together, with Immer-powered mutable syntax that the library converts to immutable updates. Use configureStore to combine slices and enable thunk middleware for asynchronous logic and Redux DevTools for inspecting state changes by default.
Before you start
- Node.js project using npm, yarn, or pnpm
- Current versions of @reduxjs/toolkit and react-redux installed
- Basic familiarity with dispatch, getState, reducers, and actions
1. Install the packages
Run this command in your project root. No administrator permissions are required for a local project install.
npm install @reduxjs/toolkit react-redux
Replace npm with your package manager if needed. This adds the official Redux Toolkit package and the React bindings.
2. Create a slice
A slice combines a reducer, its initial state, and action creators in one file. The example below tracks a list of tasks. Inside createSlice reducers, you can write code that looks like mutation, such as pushing to an array; Immer converts that into an immutable next state.
import { createSlice } from '@reduxjs/toolkit';
const tasksSlice = createSlice({
name: 'tasks',
initialState: [],
reducers: {
addTask: (state, action) => {
state.push({ id: Date.now(), text: action.payload, done: false });
},
toggleTask: (state, action) => {
const task = state.find((t) => t.id === action.payload);
if (task) {
task.done = !task.done;
}
},
},
});
export const { addTask, toggleTask } = tasksSlice.actions;
export default tasksSlice.reducer;
Place this file in your features or state folder, such as src/features/tasks/tasksSlice.js. Do not import Immer directly; createSlice handles it.
3. Configure the store
configureStore wraps createStore, combines your reducers, applies redux-thunk by default, and connects the Redux DevTools extension when available.
import { configureStore } from '@reduxjs/toolkit';
import tasksReducer from './features/tasks/tasksSlice';
export const store = configureStore({
reducer: {
tasks: tasksReducer,
},
});
Export the store so your application can import it. If you use TypeScript, you can later infer RootState and AppDispatch types from this store.
4. Provide the store to your UI
For React, wrap your application with Provider from react-redux. For plain JavaScript, import store directly and call store.dispatch.
import { Provider } from 'react-redux';
import { store } from './app/store';
function App() {
return (
<Provider store={store}>
<TaskList />
</Provider>
);
}
5. Read and update state in components
import { useDispatch, useSelector } from 'react-redux';
import { addTask, toggleTask } from '../features/tasks/tasksSlice';
export function TaskList() {
const dispatch = useDispatch();
const tasks = useSelector((state) => state.tasks);
return (
<ul>
{tasks.map((task) => (
<li key={task.id}>
<button onClick={() => dispatch(toggleTask(task.id))}>
{task.done ? 'Undo' : 'Do'}
</button>
{task.text}
</li>
))}
<button onClick={() => dispatch(addTask('Learn Redux Toolkit'))}>
Add task
</button>
</ul>
);
}
Verify the setup
- Open your browser's developer tools and confirm the Redux DevTools extension tab appears.
- Dispatch addTask and confirm the tasks array in DevTools gains a new entry.
- Confirm that dispatching an action returns a new state object reference while the previous state reference remains unchanged. You can check this in a reducer test or by logging state before and after in a component effect.
- Confirm store exposes dispatch to send actions, getState to read state, and subscribe to listen for changes.
Limitations and risks
- Never store Promises, class instances, functions, or other non-serializable values in Redux state. DevTools time-travel depends on serializable actions and state.
- The mutable syntax shown above is safe only inside createSlice reducers. Mutating state directly in a plain Redux reducer or outside createSlice violates Redux rules.
- Keep side effects such as API calls out of slice reducers. Use Redux Toolkit createAsyncThunk or RTK Query for asynchronous logic.
- configureStore enables DevTools by default; in production you may want to review whether DevTools should be disabled or restricted.
If something goes wrong
Because this procedure adds files and dependencies, rollback means removing them:
- Uninstall packages: npm uninstall @reduxjs/toolkit react-redux
- Delete the slice and store files you created.
- Revert any Provider or dispatch imports in your components.
- If you committed the changes, revert the commit in your version control system.
Result
You now have a Redux store built with Redux Toolkit. Slice reducers generate action creators automatically, and configureStore provides thunk middleware and DevTools integration without extra configuration. Verify state changes in DevTools before relying on the store in production.