Redux: State in redux/react app has a property with the name of the reducer

Created on 27 Feb 2016  路  5Comments  路  Source: reduxjs/redux

Hi,
I am creating app using Redux and React. I run into a problem where I cannot map state to component properties since the state has a property that matches the name of the reducer I used.

The reduces is created with combineReducers method

const rootReducer = combineReducers({
  appReducer
})

The initial state is

const initialState = {
  sources: [], 
  left: {}, 
  right: {},
  diff: {} 
}

However in the component function mapStateToProps:

function mapStateToProps(state) {
  return {
    sources: state.sources
  }
}

The state.sources is undefined because the value of state parameter is

{
  appReducer: {
    sources: [], 
    left: {}, 
    right: {}, 
    diff: {}
  }
}

Is this a feature of redux? So when I use more reducers all of them will add new property to state variable? Or is there something wrong on my side (I never noticed this behavior in redux tutorials).

Thanks

question

Most helpful comment

That is a feature, so you can have the same property in multiple reducers.

Imagine later on in your application you have an image grid. (totally arbitrary example) It has a list of images and a title, and the imageGridReducer initial state is:

const initialState = {
  title: 'My image grid',
  sources: [], // 馃挜
}

then you combine that reducer with your appReducer:

const rootReducer = combineReducers({
  appReducer, // { sources: [], left: {}, right: {}, diff: {} }
  imageGridReducer, // { sources: [], 馃挜  title: '' }
})

BOOM What happens now? Do the two sources get merged?? Do they get prefixed?? Do they get deleted??

combineReducers stores them under a key you specify, so you avoid this problem and don't have to worry if you have a property with the same name in another reducer.


What I'd do is:

const rootReducer = combineReducers({
  app: appReducer
});
function mapStateToProps(state) {
  return {
    sources: state.app.sources
  }
}

All 5 comments

That is a feature, so you can have the same property in multiple reducers.

Imagine later on in your application you have an image grid. (totally arbitrary example) It has a list of images and a title, and the imageGridReducer initial state is:

const initialState = {
  title: 'My image grid',
  sources: [], // 馃挜
}

then you combine that reducer with your appReducer:

const rootReducer = combineReducers({
  appReducer, // { sources: [], left: {}, right: {}, diff: {} }
  imageGridReducer, // { sources: [], 馃挜  title: '' }
})

BOOM What happens now? Do the two sources get merged?? Do they get prefixed?? Do they get deleted??

combineReducers stores them under a key you specify, so you avoid this problem and don't have to worry if you have a property with the same name in another reducer.


What I'd do is:

const rootReducer = combineReducers({
  app: appReducer
});
function mapStateToProps(state) {
  return {
    sources: state.app.sources
  }
}

@mxstbr Thanks for a quick good answer :+1:
@kfrajtak I鈥檓 closing but let us know if something is still unclear, we鈥檙e happy to help.

Much clearer now, thanks for the explanation!

No worries, glad I could help!

I also wrote an answer here: http://stackoverflow.com/a/35674297/458193

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ms88privat picture ms88privat  路  3Comments

timdorr picture timdorr  路  3Comments

dmitry-zaets picture dmitry-zaets  路  3Comments

ramakay picture ramakay  路  3Comments

parallelthought picture parallelthought  路  3Comments