React + Redux: Component does not update

前端 未结 1 698
一向
一向 2021-02-19 07:04

Trying out React + Redux, and probably am doing something obviously stupid, because a component that fires an action to fetch data over the network does not get updated (re-rend

相关标签:
1条回答
  • 2021-02-19 07:37

    I am surprized to see that the state contains reducers from the rootReducer

    This is how it works. Take a closer look at combineReducers().

    const rootReducer = combineReducers({
      navigationReducer,
      resourcesReducer
    });
    

    Recognise that it's not a list of parameters; it's a single object parameter. Perhaps it is clearer in verbose syntax:

    var rootReducer = combineReducers({
      navigationReducer: navigationReducer,
      resourcesReducer: resourcesReducer
    });
    

    The resourcesReducer key points to the state returned by the resourcesReducer() function. That is, the state variable within the resourcesReducer() is just one part of the entire state.

    The functions passed to connect() take the entire state as an argument. What yours should actually look like is this:

    export default connect(state => ({
      resources: state.resourcesReducer.resources
    }))(Showcase);
    
    0 讨论(0)
提交回复
热议问题