Skip to content

Instantly share code, notes, and snippets.

@tammyhart
Last active January 31, 2025 23:03
Show Gist options
  • Save tammyhart/c118c733501e1fad991015c230f54ab5 to your computer and use it in GitHub Desktop.
Save tammyhart/c118c733501e1fad991015c230f54ab5 to your computer and use it in GitHub Desktop.
type ConditionalWrap<T> = {
children: React.ReactNode
condition: boolean
Wrap: T
wrapProps?: T extends React.ComponentType<infer P> ? P : object
}
const ConditionalWrap = <T extends React.ElementType>({
children,
condition,
Wrap,
wrapProps,
}: ConditionalWrap<T>) =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
condition ? <Wrap {...(wrapProps as any)}>{children}</Wrap> : children
export default ConditionalWrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment