Learn

Redux Toolkit has a configureStore() method that simplifies the store setup process. configureStore() wraps around the Redux library’s createStore() method and the combineReducers() method, and handles most of the store setup for us automatically.

For example, take a look at this file which creates and exports a rootReducer

// rootReducer.js import { combineReducers } from 'redux' import todosReducer from './features/todos/todosSlice' import filtersReducer from './features/filters/filtersSlice' const rootReducer = combineReducers({ // Define a top-level state field named `todos`, handled by `todosReducer` todos: todosReducer, visibilityFilter: visibilityFilterReducer }) export default rootReducer

… and this file which creates and exports the store.

// store.js import { createStore, applyMiddleware } from 'redux' import thunkMiddleware from 'redux-thunk' import { composeWithDevTools } from 'redux-devtools-extension' import rootReducer from './reducer' const composedEnhancer = composeWithDevTools(applyMiddleware(thunkMiddleware)) const store = createStore(rootReducer, composedEnhancer) export default store

Now, let’s take a look at how we can refactor these two files using configureStore(). configureStore() accepts a single configuration object parameter. The input object should have a reducer property that defines either a function to be used as the root reducer, or an object of slice reducers which will be combined to create a root reducer.

There are many properties available in this object, but for the purposes of this lesson, just the reducer property will be sufficient.

import { configureStore } from '@reduxjs/toolkit' import todosReducer from './features/todos/todosSlice' import filtersReducer from './features/filters/filtersSlice' const store = configureStore({ reducer: { // Define a top-level state field named `todos`, handled by `todosReducer` todos: todosReducer, filters: filtersReducer } }) export default store

Note all the work that this one call to configureStore() does for us:

  • It combines todosReducer and filtersReducer into the root reducer function, which will handle a root state that looks like {todos, filters}, removing the need to call combineReducers()
  • It creates a Redux store using that root reducer, removing the need to call createStore()
  • It automatically adds the thunk middleware (which you will learn about in the next lesson!)
  • It automatically adds more middleware to check for common mistakes like accidentally mutating the state
  • It automatically sets up the Redux DevTools Extension connection

Because of how much boilerplate code we’re able to bypass with configureStore(), we can just import the individual slice reducers straight into this file instead of creating a separate file for the root reducer and having to export/import it.

Since this is as simple as switching out the store setup code, all of the application’s existing feature code will work just fine!

Let’s confirm this in the instructions below.

Instructions

1.

In the code editor, import configureStore at the top of store.js.

2.

Change the ‘favoriteRecipesReducer’ import so that it’s being imported from ../features/favoriteRecipes/favoriteRecipesSlice.js.

3.

Rewrite the default export so that it uses configureStore() instead of createStore() to create the store with the given reducers.

When you’re done, save the file and click around the application in the browser to confirm that everything still works!

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?