jrshikoku/lib/providerTreeProvider.tsx
harukin-expo-dev-env 892d567991 ts化
2025-01-22 12:03:50 +00:00

27 lines
905 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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,
]);
};