Created
October 21, 2021 10:38
-
-
Save bang9/46691ee67b77ad44443bb3f6c5f9fce8 to your computer and use it in GitHub Desktop.
JSX render concepts
This file contains 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
interface RenderNode<T extends any[] = any[]> { | |
(...args: T): React.ReactNode; | |
} | |
function iife(fn: RenderNode) { | |
return fn(); | |
} | |
function RenderListFlat<T>({ data, renderItem }: { data: T[]; renderItem: RenderNode<[item: T, index: number]> }) { | |
return <View>{data.map(renderItem)}</View>; | |
} | |
function RenderList<T>({ children, list }: { list: T[]; children: RenderNode<[item: T, index: number]> }) { | |
return <View>{list.map(children)}</View>; | |
} | |
function Renderer() { | |
return ( | |
<> | |
<RenderListFlat | |
data={new Array(5).fill("_")} | |
renderItem={(_, i) => { | |
return <SGText key={i}>RenderListFlat {i}</SGText>; | |
}} | |
/> | |
<RenderList list={new Array(5).fill("_")}> | |
{(_, i) => { | |
return <SGText key={i}>RenderList {i}</SGText>; | |
}} | |
</RenderList> | |
{new Array(5).fill("_").map((_, i) => { | |
return <SGText key={i}>Array {i}</SGText>; | |
})} | |
{iife(() => { | |
const components = []; | |
for (let i = 0; i < 5; i++) { | |
components.push(<SGText key={i}>Hello {i}</SGText>); | |
} | |
return components; | |
})} | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment