Too many React Context providers

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

    One simple solution for this is to use something similar to compose function in https://github.com/reduxjs/redux/blob/master/src/compose.js and combine all the providers together.

    provider.js

    const Providers = compose(
        AuthProvider,
        ThemeProvider,
        ChatProvider
    );
    

    also I haven't used this solution but with React's new hooks feature, instead of rendering your contexts, you can use the react hook to access it in the function definition.

提交回复
热议问题