Skip to content

Instantly share code, notes, and snippets.

@harrisrobin
Created September 12, 2022 21:18
Show Gist options
  • Save harrisrobin/71a1412ef8ec9bdb486a6b67976190b1 to your computer and use it in GitHub Desktop.
Save harrisrobin/71a1412ef8ec9bdb486a6b67976190b1 to your computer and use it in GitHub Desktop.
/* 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