Skip to content

Instantly share code, notes, and snippets.

@colinfwren
Created September 22, 2021 22:35
Show Gist options
  • Save colinfwren/3bcd26a2928b52d8fccd5236575ab068 to your computer and use it in GitHub Desktop.
Save colinfwren/3bcd26a2928b52d8fccd5236575ab068 to your computer and use it in GitHub Desktop.
Rending Union types
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