Skip to content

Integrate React with Storyblok

Use Storyblok to manage the content of your React application.

The fastest way to get started is using Storyblok’s Core Blueprint or CLI wizard:

Use Storyblok’s Core Blueprint to launch a React project, complete with a space, GitHub repository, and deployment environment.

Log in to Storyblok, select + Add Space → Start from Blueprint, and follow the steps.

Alternatively, set up the project from scratch and configure your frontend code.

This guide has been tested with the following package versions:
  • react@19.3.1
  • react-dom@19.3.1
  • react-router@8.5.3
  • @storyblok/react@8.0.0
  • @storyblok/api-client@0.14.0
  • swr@2.5.1
  • Node.js v24.22.1
  1. Create a new space

    Log in to Storyblok, select + Add Space → New Space, and follow the steps.

  2. Create a React project

    Create a new React project following the official Vite guide.

  3. Install @storyblok/react, swr, and @storyblok/api-client

    In the terminal, cd into the project, and install dependencies.

    Terminal window
    npm install @storyblok/react @storyblok/api-client swr

    This guide uses Vercel’s SWR to fetch data in the browser. The library handles loading and error states, and caches the responses.

Next, in the root of your project, create a .env file with the access token from your space.

Terminal window
VITE_STORYBLOK_DELIVERY_API_TOKEN=<YOUR-ACCESS-TOKEN>

In src, create a file named storyblok.js and initialize the Storyblok API client and React SDK.

src/storyblok.js
import { createApiClient } from "@storyblok/api-client";
import { defineStoryblokBlocks } from "@storyblok/react";
export const apiClient = createApiClient({
accessToken: import.meta.env.VITE_STORYBLOK_DELIVERY_API_TOKEN,
region: "eu", // Choose the correct region of your space.
});
export const { StoryblokBlock } = defineStoryblokBlocks(() => ({
components: {},
}));

Set the region that matches the server location of your Storyblok space.

This guide uses @storyblok/api-client to fetch data. Alternatively, use storyblok-js-client, or the native fetch API.

To fetch stories from Storyblok, add the useStoryblokStory helper that uses SWR and the apiClient.

src/storyblok.js
import { createApiClient } from "@storyblok/api-client";
import { defineStoryblokBlocks } from "@storyblok/react";
import useSWR from "swr";
export const apiClient = createApiClient({
accessToken: import.meta.env.VITE_STORYBLOK_DELIVERY_API_TOKEN,
region: "eu", // Choose the correct region of your space.
});
export const { StoryblokBlock } = defineStoryblokBlocks(() => ({
components: {},
}));
export async function fetchStory([slug, options]) {
const { data, error } = await apiClient.stories.get(slug, options);
if (error) throw error;
return data.story;
}
export function useStoryblokStory(slug, options) {
return useSWR([slug, options], fetchStory);
}

In your App.jsx file, call the useStoryblokStory hook to fetch a story’s data.

src/App.jsx
import { StoryblokBlock, useStoryblokStory } from "./storyblok";
export default function App() {
const { data: story, isLoading, error } = useStoryblokStory("home", { query: { version: "draft" } });
if (error?.response.status === 404) return <div>Story not found</div>;
if (error) return <div>Error: {error.message}</div>;
if (isLoading || !story) return <div>Loading...</div>;
return <StoryblokBlock block={story.content} />;
}

This page fetches the home story of your Storyblok space, and SWR provides the loading and error states. The API client returns a missing story as a 404 error, so the page shows a not found message for it.

StoryblokBlock renders content type and nestable blocks with the component registered under the block’s technical name. The home story uses the page content type, so StoryblokBlock looks for a page component.

Create src/storyblok/Page.jsx that renders all stories of the page content type.

src/storyblok/Page.jsx
import { StoryblokBlock } from "../storyblok";
export default function Page({ block }) {
return (
<main>
{block.body?.map((nestedBlock) => (
<StoryblokBlock block={nestedBlock} key={nestedBlock._uid} />
))}
</main>
);
}

Use StoryblokBlock to iterate through the body field and render the blocks in it.

Stories can have a body field that contains an array of custom type blocks, such as Feature, Teaser, and Grid.

Create the code for these components as follows.

src/storyblok/Feature.jsx
export default function Feature({ block }) {
return (
<div className="feature">
<span>{block.name}</span>
</div>
);
}
src/storyblok/Teaser.jsx
export default function Teaser({ block }) {
return (
<div className="teaser">
<h2>{block.headline}</h2>
</div>
);
}
src/storyblok/Grid.jsx
import { StoryblokBlock } from "../storyblok";
export default function Grid({ block }) {
return (
<div className="grid">
{block.columns?.map((nestedBlock) => (
<StoryblokBlock block={nestedBlock} key={nestedBlock._uid} />
))}
</div>
);
}

Similar to Page.jsx, Grid.jsx iterates over the columns block field.

Register the components in storyblok.js. The key must match the block’s technical name.

src/storyblok.js
import { createApiClient } from "@storyblok/api-client";
import { defineStoryblokBlocks } from "@storyblok/react";
import useSWR from "swr";
import Page from "./storyblok/Page";
import Teaser from "./storyblok/Teaser";
import Feature from "./storyblok/Feature";
import Grid from "./storyblok/Grid";
export const apiClient = createApiClient({
accessToken: import.meta.env.VITE_STORYBLOK_DELIVERY_API_TOKEN,
region: "eu", // Choose the correct region of your space.
});
export const { StoryblokBlock } = defineStoryblokBlocks(() => ({
components: {},
components: {
page: Page,
teaser: Teaser,
feature: Feature,
grid: Grid,
},
}));

Run the server and visit the site in your browser.

Terminal window
npm run dev

Was this page helpful?

What went wrong?

This site uses reCAPTCHA and Google's Privacy Policy (opens in a new window).Terms of Service (opens in a new window) apply.