Redux: Naming reducers for combineReducers

Created on 20 Sep 2015  路  2Comments  路  Source: reduxjs/redux

This may not be an issue actually. Is the name of the imported reference equals to the property name of "state" when I call mapStateToProps? e.g. I need to use "state.someReducers". I am still new to redux, perhaps I missed something in documentation.

import someReducers from './someReducers';

const rootReducer = combineReducers({
  someReducers
});

Why don't we do it more explicit?

import someReducers from './someReducers';

const rootReducer = combineReducers({
  items: someReducers
});

Most helpful comment

There's a balance between being explicit in examples and showing what we believe to be best practices. We don't really want to encourage people to name reducer files someReducers; the convention we encourage is to match reducer name to the state field name. But then writing { something: something } is redundant in ES6 so we don't do that either.

All 2 comments

Yes, the shape of the object passed into combineReducers matches the store state that those reducers will operate on. If we do { itemsReducers }, that desugars to { itemsReducers: itemsReducers }, which is probably not what you want. I normally do something like this:

import items from './someReducers';

const rootReducer = combineReducers({
  items
});

Your import name can be whatever you want it to be, so you can match it to your state shape.

There's a balance between being explicit in examples and showing what we believe to be best practices. We don't really want to encourage people to name reducer files someReducers; the convention we encourage is to match reducer name to the state field name. But then writing { something: something } is redundant in ES6 so we don't do that either.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

captbaritone picture captbaritone  路  3Comments

ilearnio picture ilearnio  路  3Comments

timdorr picture timdorr  路  3Comments

ramakay picture ramakay  路  3Comments

jimbolla picture jimbolla  路  3Comments