I like to use contexts in my React and React Native projects, which means multiple context providers per project. As a result, the root of my app often looks like this:
<ContextA.Provider value={valueA}>
<ContextB.Provider value={valueB}>
<ContextC.Provider value={valueC}>
// ...and so on until rendering actual app content
</ContextC.Provider>
</ContextB.Provider>
</ContextA.Provider>
This creates a pyramid of providers that looks and feels like bad style/practice.
I could lump my context values together into one big provider:
<BigContext.Provider value={ valueA, valueB, valueC }>
/// app content
</BigContext.Provider>
...but there's a few good reasons to want to keep contexts separate - mainly preventing components that are only interested in valueA
from re-rendering when only valueB
changes, for example.
Even without contexts, you can still have providers from different packages stack up into their own pyramids. Here's the root of one of my React Native apps, for example:
<DataContext.Provider value={data}>
<AppearanceProvider>
<SafeAreaProvider>
<NavigationContainer>
<Tab.Navigator>
// tab screens here
</Tab.Navigator>
</NavigationContainer>
</SafeAreaProvider>
</AppearanceProvider>
</DataContext.Provider>
Is there a clean way to "collapse", or somehow avert, these Pyramids of Doom?