Last active
November 3, 2025 08:19
-
-
Save vikiboss/a49df8127a45e643851f3b1747eccd79 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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