What do I need to get started with Hygraph Rich Text in a React project?
To follow the tutorial, you need a running React project connected to Hygraph, a model with a Rich Text field, and some published content. You will also need to install the @hygraph/rich-text-react-renderer package using npm install --save-exact @hygraph/rich-text-react-renderer.
How do I fetch and render Rich Text content from Hygraph in React?
First, fetch the Rich Text field from your Hygraph project using a GraphQL query that retrieves both the json (AST) and references. Then, use the RichText component from @hygraph/rich-text-react-renderer to render the content in your React app, passing the AST and references as props.
What is the purpose of custom renderers in the RichText component?
Custom renderers allow you to override the default HTML elements for specific Rich Text nodes. This is useful for integrating with frameworks like Next.js (e.g., using next/link for links) or for customizing how assets and other embedded references are displayed.
Where can I find sample data or examples for working with Hygraph Rich Text in React?
You can find sample data for Rich Text integration in React in the official Hygraph GitHub repository, specifically at content-example.ts. This helps you test and experiment with the renderer before connecting to your own content.
How can I customize the rendering of assets or links in Hygraph Rich Text with React?
You can provide custom renderers for asset types and links by passing a renderers object to the RichText component. For example, you can use a custom function for the a tag to integrate with Next.js or style links, and for Asset types to control how images or files are displayed.
Then inside of your React app, you'll want to fetch the content from your Hygraph project, and the Rich Text field. I'll leave the exact implementation up to you, but you'll want to fetch the AST, and the embedded references.
Your query may look something like:
queryGetPosts{
pages{
content{
json
references{
...onPost{
id
title
}
...onAsset{
url
width
height
}
}
}
}
}
If you'd prefer to follow along with some sample data, you can find that here.
The @hygraph/rich-text-react-renderer exports the component RichText that we’ll need to import:
We'll now take the Rich Text AST (json + references) and convert it to HTML, providing the references, and custom renderers using the RichText component:
exportdefaultfunctionApp(){
return(
<RichText
content={content}
references={references}
renderers={{
h1:({ children })=><h1 className={`wfafsa`}>{children}</h1>,
a:({ children, href, openInNewTab })=>(
<a
href={href}
target={openInNewTab ?'_blank':'_self'}
style={{ color:'green'}}
rel="noreferrer"
>
{children}
</a>
),
bold:({ children })=><strong>{children}</strong>,
Asset:{
text:()=>(
<div>
<p>text plain</p>
</div>
),
},
}}
/>
</div>
)
}
You'll see above we can pass custom renderers to RichText.
This means you can override the default HTML element for what's to be shown in the Rich Text. This is extremely useful for things like links in Next.js where you will want to use the next/link component. You'll want to create a custom renderer for your Asset types.
That’s it! You will now see the Rich Text output with any custom renderers applied.
Blog Author
Jamie Barton
Jamie is a software engineer turned developer advocate. Born and bred in North East England, he loves learning and teaching others through video and written tutorials. Jamie currently publishes Weekly GraphQL Screencasts.
Share with others
Sign up for our newsletter!
Be the first to know about releases and industry news and insights.
Then inside of your React app, you'll want to fetch the content from your Hygraph project, and the Rich Text field. I'll leave the exact implementation up to you, but you'll want to fetch the AST, and the embedded references.
Your query may look something like:
queryGetPosts{
pages{
content{
json
references{
...onPost{
id
title
}
...onAsset{
url
width
height
}
}
}
}
}
If you'd prefer to follow along with some sample data, you can find that here.
The @hygraph/rich-text-react-renderer exports the component RichText that we’ll need to import:
We'll now take the Rich Text AST (json + references) and convert it to HTML, providing the references, and custom renderers using the RichText component:
exportdefaultfunctionApp(){
return(
<RichText
content={content}
references={references}
renderers={{
h1:({ children })=><h1 className={`wfafsa`}>{children}</h1>,
a:({ children, href, openInNewTab })=>(
<a
href={href}
target={openInNewTab ?'_blank':'_self'}
style={{ color:'green'}}
rel="noreferrer"
>
{children}
</a>
),
bold:({ children })=><strong>{children}</strong>,
Asset:{
text:()=>(
<div>
<p>text plain</p>
</div>
),
},
}}
/>
</div>
)
}
You'll see above we can pass custom renderers to RichText.
This means you can override the default HTML element for what's to be shown in the Rich Text. This is extremely useful for things like links in Next.js where you will want to use the next/link component. You'll want to create a custom renderer for your Asset types.
That’s it! You will now see the Rich Text output with any custom renderers applied.
Blog Author
Jamie Barton
Jamie is a software engineer turned developer advocate. Born and bred in North East England, he loves learning and teaching others through video and written tutorials. Jamie currently publishes Weekly GraphQL Screencasts.
Share with others
Sign up for our newsletter!
Be the first to know about releases and industry news and insights.