Created
September 22, 2021 22:35
-
-
Save colinfwren/3bcd26a2928b52d8fccd5236575ab068 to your computer and use it in GitHub Desktop.
Rending Union types
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 TypeOne({ title, subtitle }) { | |
return ( | |
<h1>{title}</h1> | |
<p>{subtitle}</p> | |
) | |
} | |
function TypeTwo({ image, content }) { | |
return ( | |
<div> | |
<img src={image} /> | |
<p>{content}</p> | |
</div> | |
) | |
} | |
export default function Page({ data: { page }) { | |
const blocks = page.blocks.map((block) => { | |
const Block = block.remoteTypeName === 'One' ? TypeOne : TypeTwo | |
return <Block data={block} /> | |
}) | |
return ( | |
<> | |
<h1>{page.title}</h1> | |
{blocks} | |
</> | |
) | |
} | |
export const query = graphql` | |
query PageQuery($pageId: String!) { | |
page: graphCmsPage(id: { eq: $pageId }) { | |
title | |
blocks { | |
... on GraphCMS_One { | |
id | |
remoteTypeName | |
title | |
subtitle | |
} | |
... on GraphCMS_Two { | |
id | |
remoteTypeName | |
image | |
content | |
} | |
} | |
} | |
} | |
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment