This commit is contained in:
harukin-expo-dev-env
2025-01-22 12:03:50 +00:00
parent 294b95967f
commit 892d567991
8 changed files with 26 additions and 27 deletions

View File

@@ -0,0 +1,26 @@
import React, { FC } from "react";
export const buildProvidersTree:FC<any> = (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 }} />
</FirstProvider>
),
...providers,
]);
};