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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay