Too many React Context providers

前端 未结 5 1830
渐次进展
渐次进展 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:13

    Solution with for loop:

    export const provider = (provider, props = {}) => [provider, props];
    
    export const ProviderComposer = ({providers, children}) => {
        for (let i = providers.length - 1; i >= 0; --i) {
            const [Provider, props] = providers[i];
            children = {children}
        }
        return children;
    }
    

    Usage:

    
        
    
    

提交回复
热议问题