I am attempting to tidy my App.js file by using a function to inject a nested array of providers.
Previously I had something that looked like:
<CreditsProvider>
<BundlesProvider>
<ContactsProvider>
<PhoneVerificationProvider>
<PurchaseProvider>
<RanksProvider>
<RoleWizardProvider>
<BasesProvider>
<DepsProvider>
<TitlesProvider>
<UrlsProvider>
<Main />
</UrlsProvider>
</TitlesProvider>
</DepsProvider>
</BasesProvider>
</RoleWizardProvider>
</RanksProvider>
</PurchaseProvider>
</PhoneVerificationProvider>
</ContactsProvider>
</BundlesProvider>
</CreditsProvider>
I built a function to do this automatically:
export function BuildProviderTree(providers) {
if (providers.length === 1) {
return providers[0];
}
const A = providers.shift();
const B = providers.shift();
return BuildProviderTree([
({ children }) => (
<A>
<B>{children}</B>
</A>
),
...providers,
]);
}
Back in App.js I fee the function the list of providers and add it to my app.
const Providers = BuildProviderTree(
...[
HeaderBarProvider,
CreditsProvider,
BundlesProvider,
ContactsProvider,
PhoneVerificationProvider,
PurchaseProvider,
RanksProvider,
RoleWizardProvider,
BasesProvider,
DepsProvider,
TitlesProvider,
UrlsProvider,
]
);
<NotificationBarProvider>
<NotificationBar />
<Providers>
<Main />
</Providers>
</NotificationBarProvider>
Everything works fine, however when I use my React dev tools to inspect the component structure I notice a bunch of nested Anonymous elements.
I have tried changing my export declaration as mentioned in a comment and this didn't work.
As I dug in a bit further I noticed all of the Anonymous elements are coming from the <A> elements in the function while the named providers are the <B> elements.
