These integration guides are not official documentation and the Strapi Support Team will not provide assistance with them.
Why Use React Router 7?
React Router 7 introduces several enhancements that aim to improve developer workflow and application performance. Here's a breakdown:
For Developers:
- Simplified Installation: React Router 7 unifies core functionality into a single package, react-router, streamlining installation and management.
- Streamlined Data Handling: The removal of the json() utility function in favor of manual response object construction offers greater flexibility and transparency in data handling.
- Simplified defer Functionality: The defer function, used for delayed data loading, has been simplified, reducing boilerplate code.
- Framework Mode for Full-Stack Development: React Router 7 introduces a "framework mode" enabling full-stack React application development with features like server-side rendering (SSR), code splitting, and file-based routing, all within a familiar tool.
- Improved Nested Routing with Layout Routes: Layout routes, using the Outlet component, allow developers to define shared layouts for nested routes, reducing code duplication and promoting a clear separation of layout and content.
- Enhanced Type Safety: Improved type safety for route parameters, loader data, and actions ensures better developer experience and reduces runtime errors.
Why Use Strapi?
Strapi is the leading open-source headless CMS offering features, like customizable APIs, role-based permissions, multilingual support, etc. It simplifies content management and integrates effortlessly with modern frontend frameworks.
Explore the Strapi documentation for more details.
Strapi 5 Highlights
The out-of-the-box Strapi features allow you to get up and running in no time: 1. Single types: Create one-off pages that have a unique content structure. 2. Draft and Publish: Reduce the risk of publishing errors and streamline collaboration. 3. 100% TypeScript Support: Enjoy type safety & easy maintainability 4. Customizable API: With Strapi, you can just hop in your code editor and edit the code to fit your API to your needs. 5. Integrations: Strapi supports integrations with Cloudinary, SendGrid, Algolia, and others. 6. Editor interface: The editor allows you to pull in dynamic blocks of content. 7. Authentication: Secure and authorize access to your API with JWT or providers. 8. RBAC: Help maximize operational efficiency, reduce dev team support work, and safeguard against unauthorized access or configuration modifications. 9. i18n: Manage content in multiple languages. Easily query the different locales through the API. 10. Plugins: Customize and extend Strapi using plugins.
Learn more about Strapi 5 feature.
See Strapi in action with an interactive demo
Setup Strapi 5 Headless CMS
We are going to start by setting up our Strapi 5 project with the following command:
🖐️ Note: make sure that you have created a new directory for your project.
You can find the full documentation for Strapi 5 here.
Install Strapi
npx create-strapi-app@latest server
You will be asked to choose if you would like to use Strapi Cloud we will choose to skip for now.
Strapi v5.6.0 🚀 Let's create your new project
We can't find any auth credentials in your Strapi config.
Create a free account on Strapi Cloud and benefit from:
- ✦ Blazing-fast ✦ deployment for your projects
- ✦ Exclusive ✦ access to resources to make your project successful
- An ✦ Awesome ✦ community and full enjoyment of Strapi's ecosystem
Start your 14-day free trial now!
? Please log in or sign up.
Login/Sign up
❯ Skip
After that, you will be asked how you would like to set up your project. We will choose the following options:
? Do you want to use the default database (sqlite) ? Yes
? Start with an example structure & data? Yes <-- make sure you say yes
? Start with Typescript? Yes
? Install dependencies with npm? Yes
? Initialize a git repository? Yes
Once everything is set up and all the dependencies are installed, you can start your Strapi server with the following command:
cd server
npm run develop
You will be greeted with the Admin Create Account screen.
Go ahead and create your first Strapi user. All of this is local so you can use whatever you want.
Once you have created your user, you will be redirected to the Strapi Dashboard screen.
Publish Article Entries
Since we created our app with the example data, you should be able to navigate to your Article collection and see the data that was created for us.
Now, let's make sure that all of the data is published. If not, you can select all items via the checkbox and then click the Publish button.
Enable API Access
Once all your articles are published, we will expose our Strapi API for the Articles Collection. This can be done in Settings -> Users & Permissions plugin -> Roles -> Public -> Article.
You should have find
and findOne
selected. If not, go ahead and select them.
Test API
Now, if we make a GET
request to http://localhost:1337/api/articles
, we should see the following data for our articles.
🖐️ Note: The article covers (images) are not returned. This is because the REST API by default does not populate any relations, media fields, components, or dynamic zones.. Learn more about REST API: Population & Field Selection.
So, let's get the article covers by using the populate=*
parameter: http://localhost:1337/api/articles?populate=*
Nice, now that we have our Strapi 5 server setup, we can start to setup our React Router 7 client.
Getting Started with React Router 7
I will walk you through the steps to setup a new React Router 7 project. But here is the link to the React Router 7 Docs that I used for reference.
React Router 7 Installation and Initial Setup
We are going to got the React Router 7 Framework Route.
Make sure that you are in the root
directory of your project and run the following command to install React Router 7.
npx create-react-router@latest client
Say Yes to all the default options.
Once everything is installed, you can start your React Router 7 server with the following command:
cd client
npm run dev
Nice, now that we have our React Router 7 client setup, we can start to integrate it with our Strapi 5 server.
React Router 7 File Based Routing
We are going to opt in to use the File Based Routing Convention for our React Router 7 application.
You can read more about it here.
In order to do this, we must first install the following package:
npm i @react-router/fs-routes
Next, let's navigate to the app/routes.tsx
file and update the file to the following:
1// https://reactrouter.com/how-to/file-route-conventions
2
3import { type RouteConfig } from "@react-router/dev/routes";
4import { flatRoutes } from "@react-router/fs-routes";
5
6export default flatRoutes() satisfies RouteConfig;
And finally, in the client/app/routes
folder, rename the home.tsx
file to _index.tsx
.
and update the file to the following:
1//old:
2import type { Route } from "./+types/home";
3
4// updated:
5import type { Route } from "./+types/_index";
Now restart your React Router 7 and everything should still be working.
If you wanted to add a new route, you can do so by creating a new file in the app/routes
folder.
Let's do a simple example.
In the app/routes
folder, create a new file called about.tsx
.
And update the file to the following:
1import type { Route } from "./+types/about";
2
3export function meta({}: Route.MetaArgs) {
4 return [
5 { title: "About" },
6 { name: "description", content: "About Page" },
7 ];
8}
9
10export default function About() {
11 return <div className="container mx-auto p-4">This is the about page</div>;
12}
Now restart your React Router 7 and you should be able to navigate to the about page at http://localhost:5173/about
.
Nice, now that we have a basic route setup, we can start to integrate it with our Strapi 5 server.
Fetching Data from Strapi 5 with React Router 7
We are going to opt in to use server loader to fetch data from our Strapi 5 server.
You can read more about it here.
To make this work, we need to update our app/routes/_index.tsx
let's update the file with the following code:
1import qs from "qs";
2
3import type { Route } from "./+types/_index";
4import { ArticleCard } from "../components/article-card";
5
6interface Article {
7 id: number;
8 documentId: string;
9 title: string;
10 description: string;
11 slug: string;
12 createdAt: string;
13 updatedAt: string;
14 publishedAt: string;
15 cover: {
16 url: string;
17 alternativeText: string;
18 };
19}
20
21interface ArticleResponse {
22 data: Article[];
23}
24
25interface LoaderData {
26 articlesData: ArticleResponse;
27}
28
29// Server Side Loader
30export async function loader({ params }: Route.LoaderArgs) {
31 const BASE_URL = import.meta.env.VITE_STRAPI_URL || "http://localhost:1337";
32 const path = "/api/articles";
33 const url = new URL(path, BASE_URL);
34
35 url.search = qs.stringify({
36 populate: {
37 cover: {
38 fields: ["url", "alternativeText"],
39 },
40 },
41 });
42
43 const articlesData = await fetch(url.href);
44 const data = await articlesData.json();
45 return { articlesData: data as ArticleResponse };
46}
47
48export function meta(/*{}: Route.MetaArgs */) {
49 return [
50 { title: "Home | React Router 7" },
51 { name: "description", content: "Home Page" },
52 ];
53}
54
55export default function IndexRoute({ loaderData }: { loaderData: LoaderData }) {
56 if (!loaderData) return <p>No data found</p>;
57 return (
58 <div className="container mx-auto px-4 py-8">
59 <h1 className="text-3xl font-bold mb-6">
60 React Router 7 and Strapi 5 Demo
61 </h1>
62 <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
63 {loaderData.articlesData.data.map((article: Article) => (
64 <ArticleCard
65 key={article.id}
66 title={article.title}
67 description={article.description}
68 publishedAt={article.publishedAt}
69 slug={article.slug}
70 cover={article.cover}
71 />
72 ))}
73 </div>
74 </div>
75 );
76}
Before this will work, we need to add two more components to our project, and install the qs
package that will help us with the query string.
In our projects app
folder, create a new folder called components
. And let's add two new components to our project.
We will start with the StrapiImage
component.
Create a new file called strapi-image.tsx
and update the file with the following code:
1interface StrapiImageProps {
2 src: string;
3 alt: string | null;
4 height?: number;
5 width?: number;
6 className?: string;
7}
8
9const STRAPI_API_URL = process.env.STRAPI_API_URL ?? "http://localhost:1337";
10
11export function getStrapiMedia(url: string | null) {
12 if (url == null) return null;
13 if (url.startsWith("data:")) return url;
14 if (url.startsWith("http") || url.startsWith("//")) return url;
15 return `${STRAPI_API_URL}${url}`;
16}
17
18export function StrapiImage({
19 src,
20 alt,
21 className,
22 ...rest
23}: Readonly<StrapiImageProps>) {
24 const imageUrl = getStrapiMedia(src);
25 if (!imageUrl) return null;
26 return (
27 <img
28 src={imageUrl}
29 alt={alt ?? "No alternative text provided"}
30 className={className}
31 {...rest}
32 />
33 );
34}
Now let's create the ArticleCard
component.
Create a new file called article-card.tsx
and update the file with the following code:
1import { StrapiImage } from "./strapi-image";
2
3export interface ArticleCardProps {
4 title: string;
5 description: string;
6 publishedAt: string;
7 slug: string;
8 cover: {
9 url: string;
10 alternativeText: string;
11 };
12}
13
14export function ArticleCard({
15 title,
16 description,
17 publishedAt,
18 slug,
19 cover,
20}: Readonly<ArticleCardProps>) {
21 return (
22 <div className="bg-white rounded-lg shadow-md overflow-hidden hover:shadow-lg transition-shadow duration-300">
23 <div className="aspect-w-16 aspect-h-9">
24 <StrapiImage
25 src={cover.url}
26 alt={cover.alternativeText}
27 className="object-cover w-full h-full"
28 />
29 </div>
30 <div className="p-6">
31 <h2 className="text-xl font-bold text-gray-800 mb-2">{title}</h2>
32 <p className="text-gray-600 mb-4 line-clamp-2">{description}</p>
33 <div className="flex justify-between items-center text-sm text-gray-500">
34 <span>{new Date(publishedAt).toLocaleDateString()}</span>
35 </div>
36 </div>
37 </div>
38 );
39}
And finally, let's add the qs
package to our project.
npm i qs
npm i @types/qs
Now restart your React Router 7 app and you Strapi 5 app and you should be able to the following screen.
Awesome, great job!
Github Project Repo
You can find the complete code for this project in the following Github repo.
Strapi Open Office Hours
If you have any questions about Strapi 5 or just would like to stop by and say hi, you can join us at Strapi's Discord Open Office Hours Monday through Friday at 12:30 pm - 1:30 pm CST: Strapi Discord Open Office Hours
For more details, visit the Strapi documentation and React Router 7 documentation.