App.jsの分離、ProviderTreeの導入

This commit is contained in:
harukin-expo-dev-env
2024-08-20 01:48:51 +00:00
parent b620f5cf75
commit 7781cf43e8
3 changed files with 104 additions and 73 deletions

View File

@@ -0,0 +1,25 @@
export const buildProvidersTree = (providers) => {
// 基本ケースContextProviderが1つしか残っていない場合、それを返して終了する
if (providers.length === 1) {
return providers[0];
}
// 配列から最初の2つのContextProviderを取り出す
const FirstProvider = providers.shift();
const SecondProvider = providers.shift();
// 十分な数のContextProviderがあるかどうかを確認
if (FirstProvider === undefined || SecondProvider === undefined) {
throw new Error("ContextProviderが不足しています");
}
// 最初の2つのContextProviderをネストした新しいContextProviderを作成し、再帰する
return buildProvidersTree([
({ children }) => (
<FirstProvider>
<SecondProvider>{children}</SecondProvider>
</FirstProvider>
),
...providers,
]);
};