Too many React Context providers

前端 未结 5 1823
渐次进展
渐次进展 2021-01-31 17:39

New to react here and trying to wrap my head round the new Context API (I haven\'t looked into Redux etc. yet).

Seems I can do much of what I need to do, but I\'m going

5条回答
  •  别那么骄傲
    2021-01-31 18:22

    Few lines of code solve your problem.

    import React from "react"
    import _ from "lodash"
    
    /**
     * Provided that a list of providers [P1, P2, P3, P4] is passed as props,
     * it renders
     *
     *    
            
              
                
                  {children}
                
              
            
          
     *
     */
    
    export default function ComposeProviders({ Providers, children }) {
      if (_.isEmpty(Providers)) return children
    
      return _.reverse(Providers)
        .reduce((acc, Provider) => {
          return {acc}
        }, children)
    }
    

提交回复
热议问题