Created
May 28, 2021 16:36
-
-
Save kyleshevlin/a5f8c8a8b3857ed61de3b21b2889411a to your computer and use it in GitHub Desktop.
Sometimes createElement makes more sense
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
import { ComponentType, createElement, CSSProperties } from 'react' | |
interface HasKey { | |
key: string | |
} | |
type HomogenousListProps<T> = { | |
as?: 'ol' | 'ul' | |
component: ComponentType<T> | |
items?: Array<T> | |
style?: CSSProperties | |
} | |
function HomogenousList<T extends HasKey>({ | |
as = 'ul', | |
component, | |
items = [], | |
style, | |
}: HomogenousListProps<T>) { | |
return createElement( | |
as, | |
{ style }, | |
items.map((item: T) => createElement(component, item)) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment