How to handle global state data into deeply nested components in Redux?

后端 未结 2 1592
青春惊慌失措
青春惊慌失措 2021-01-31 22:30

So say you have a chat aplication with this component structure:


  ...
  ...<         


        
相关标签:
2条回答
  • 2021-01-31 22:44

    (UPDATE: Having spent some time on option 4, I personally think it's the way to go. I published a library, react-redux-controller built around this approach.)

    There are a few approaches that I know of from getting data from your root component, down to your leaf components, through the branches in the middle.

    Props chain

    The Redux docs, in the context of using react-redux, suggest passing the data down the whole chain of branches via props. I don't favor this idea, because it couples all the intermediate branch components to whatever today's app structure is. On the bright side, your React code would be fairly pure, and only coupled to Redux itself at the top level.

    Selectors in all components

    Alternatively, you could use connect to make data from your Redux store available, irrespective of where you are in the component tree. This decouples your components from one another, but it couples everything to Redux. I would note that the principle author of Redux is not necessarily opposed to this approach. And it's probably more performant, as it prevents re-renders of intermediary components due to changes in props they don't actually care about.

    React children

    I haven't thought a great deal about doing things this way, but you could describe your whole app structure at the highest level as nested components, passing in props directly to remote descendants, and using children to render injected components at the branch levels. However, taken to the extreme, this would make your container component really complicated, especially for intermediate components that have children of more than one type. Not sure if this is really viable at all for that reason.

    React context

    As first mentioned by @mattclemens, you can use the experimental context api to decouple your intermediate components. Yes, it's "experimental". Yes, the React team definitely doesn't seem to be in love with it. But keep in mind that this is exactly what Redux's connect uses to inject dispatch and props from selectors.

    I think it strikes a nice balance. Components remain decoupled, because branch components don't need to care about the descendants' dependencies. If you only use connect at the root to set up the context, then all the descendents only need to couple to React's context API, rather than Redux. Components can freely be rearranged, as long as some ancestor is setting the required context properties. If the only component that sets context is the root component, this is trivially true.

    The React team compares using context to global variables, but that feel like an exaggeration. It seems a lot more like dependency injection to me.

    0 讨论(0)
  • 2021-01-31 23:10

    For information that is global to all of your "dumb" components, you could use react contexts.

    A contrived example

    // redux aware component
    var ChatApp = React.createClass({
      childContextTypes: {
        language: React.PropTypes.string
      },
      getChildContext: function() {
        // or pull from your state tree
        return {language: "en"};
      },
      ...
    }
    
    // dumb components
    var ExDumb = React.createClass({
      contextTypes: {
        language: React.PropTypes.string
      },
    
      render: function() {
        var lang = this.context.language;
        return ( <div /> );
       }
     });
    

    In response to the comments, redux uses this context approach in their react-redux library.

    And more abstractly for use outside of react, you could use some sort of pluck or selector function on the state tree, and return only a subset of the global state needed by dumb components.

    0 讨论(0)
提交回复
热议问题