Skip to content

Instantly share code, notes, and snippets.

@vikiboss
Last active November 3, 2025 08:19
Show Gist options
  • Select an option

  • Save vikiboss/a49df8127a45e643851f3b1747eccd79 to your computer and use it in GitHub Desktop.

Select an option

Save vikiboss/a49df8127a45e643851f3b1747eccd79 to your computer and use it in GitHub Desktop.
function When<T>(props: {
condition: T
children?: React.ReactNode | ((data: NonNullable<T>) => React.ReactNode)
fallback?: React.ReactNode
}) {
const { condition, children, fallback } = props
if (condition) {
if (typeof children === 'function') {
return children?.(condition) ?? null
}
return children ?? null
}
return fallback ?? null
}
export function App() {
const data: string | null = Math.random() > 0.5 ? 'hi' : null
return (
<When condition={data} fallback="no data found">
{(data) => <div>Child {data.toUpperCase()}</div>}
</When>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment