Created
September 12, 2022 21:18
-
-
Save harrisrobin/71a1412ef8ec9bdb486a6b67976190b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable @typescript-eslint/no-explicit-any */ | |
import React from "react" | |
import { SafeAreaProvider } from "react-native-safe-area-context" | |
// Cache the wrapper component to avoid creating a new one every time a screen | |
// is created. | |
const componentsCache = new WeakMap< | |
React.ComponentType<any>, | |
React.ComponentType<any> | |
>() | |
export function getScreenComponent( | |
getComponent: () => React.ComponentType<any>, | |
): () => React.ComponentType<any> { | |
return () => { | |
const Component = getComponent() | |
const CachedComponent = componentsCache.get(Component) | |
if (CachedComponent != null) { | |
return CachedComponent | |
} | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
function ScreenComponent(props: any) { | |
return ( | |
<SafeAreaProvider> | |
<Component {...props} /> | |
</SafeAreaProvider> | |
) | |
} | |
componentsCache.set(Component, ScreenComponent) | |
return ScreenComponent | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment