Content Modeling in React
Learn how to handle different content type blocks, render rich text, and use story references to manage content globally.
Alternatively, in your existing space, create a new content type block article
and an “Articles” folder with content. The article content type block should have the following fields:
title
: Textcontent
: Rich text
Create an article-overview
story as a page type content.
Finally, create a featured-articles
nestable block with the following field:
articles
: References
Add a new featured-articles
block to the body
field of the home story and select some articles to be featured.
Fetch and list all articles
Section titled “Fetch and list all articles”Create a new storyblok/ArticleOverview.jsx
file to get all stories from this new content type.
import { useStoryblokApi } from '@storyblok/react';import { use, Suspense, useMemo } from 'react';import { Link } from 'react-router';
export default function ArticleOverview() { const storyblokApi = useStoryblokApi(); const articlesPromise = useMemo(() => { return storyblokApi.getAll('cdn/stories', { version: 'draft', starts_with: 'articles', content_type: 'article', }); }, [storyblokApi]);
return ( <section> <h2>Articles</h2> <Suspense fallback={<p>Loading...</p>}> <ul> <ArticleList getArticles={articlesPromise} /> </ul> </Suspense> </section> );}
export function ArticleList({ getArticles }) { const articles = use(getArticles);
return articles.map((article) => ( <li key={article.uuid}> <Link to={`/${article.full_slug}`}>{article.name}</Link> </li> ));}
Using the starts_with
parameter, only stories from the “Articles” folder are fetched. Using the content_type
parameter, the results are restricted to stories of the content type article
.
Now, the article overview page shows a list of links to all articles.
Create the article block
Section titled “Create the article block”Add a new src/storyblok/Article.jsx
component to render the new article content type.
import { storyblokEditable, StoryblokRichText } from '@storyblok/react';
export default function Article({ blok }) { return ( <div {...storyblokEditable(blok)} className="article"> <h1>{blok.title}</h1> <StoryblokRichText doc={blok.content} /> </div> );}
To render rich text fields, use the StoryblokRichText
component provided by the @storyblok/react
module.
<aside> 💡
Learn more about handling rich text in Storyblok in the fields concept and the @storyblok/richtext
reference.
</aside>
When clicking on links present in the article overview page, an article page renders correctly.
Handle referenced stories
Section titled “Handle referenced stories”In the src/App.jsx
data file, set the resolve_relations
parameter to get the full object response of referenced stories.
import { StoryblokComponent, useStoryblok } from '@storyblok/react';import { useParams } from 'react-router';
export default function App() { const { '*': slug } = useParams(); const story = useStoryblok(slug || "home", { version: 'draft', resolve_relations: 'featured-articles.articles', });
if (!story?.content) { return <div>Loading...</div>; }
return <StoryblokComponent blok={story.content} />;}
Next, create a new storyblok/FeaturedArticles.jsx
component.
import { storyblokEditable } from '@storyblok/react';import { Link } from 'react-router';
export default function FeaturedArticles({ blok }) { return ( <section {...storyblokEditable(blok)} className="featured-articles"> <h2>Featured Articles</h2> <ul> {blok.articles.map((article) => ( <li key={article.uuid}> <Link to={`/${article.full_slug}`}>{article.name}</Link> </li> ))} </ul> </section> );}
Now, this component will render links to the featured articles in the home page of your project.
Related resources
Section titled “Related resources”Get in touch with the Storyblok community