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

前端 未结 2 567
自闭症患者
自闭症患者 2021-02-01 07:01

I am creating an 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 red

2条回答
  •  一生所求
    2021-02-01 07:33

    Actually, I believe your initial state would be:

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

    This is because combineReducers works by taking the name of the reducer, and mapping its contents to that name.

    Also, just a note, but if you're going to use more than 1 reducer, the names of your reducers should be more specific than appReducer, and (just my personal opinion) they don't need the word reducer. A typical app might look like this:

    combineReducers({
      user: userReducer,
      messages: messagesReducer,
      notifications: notificationsReducer
    });
    

    Then, your state could be accessed like:

    state.user.email
    state.messages[0]
    

提交回复
热议问题