DEV Community

Cover image for Root Reducer
Cathy Diaz
Cathy Diaz

Posted on

Root Reducer

In order to create a store for redux, you need to use the createStore method. This method takes in three arguments. One argument, the reducer, is required while the other two (initial state & middleware) are optional.

create store

Today I will be talking about the required argument, the reducer. In my project I only have one reducer but have created a root reducer in case I decide to add more reducers in the future.

A root reducer is necessary when you have more than one model in your code. More than one model is more than one reducer and the create store method does not take in multiple reducers. So what needs to be done is to create a root reducer that holds multiple reducers.

Here I show you how I created my root reducer using the combine reducers method. This method is what allows me to combine multiple reducers to one main reducer.

combineReducers

When I decide to add another model, I will simply import the file to my root reducer and add it inside my combine reducers method, right below the reducer you see there.

And just like that you're able to have multiple reducers in your store.

done

Top comments (0)