VOOZH about

URL: https://strapi.io/blog/build-a-blog-with-react-strapi-and-apollo

⇱ Strapi - Build a blog using Strapi, React and Apollo


Higher Quality, Stronger Performance, Increased Stability, Better Developer Experience, discover everything we've shipped recently!

  • Last updated: December 22, 2022 (Strapi v4 era)
  • 13 min read

Build a blog with React, Strapi and Apollo

January 8, 2020

👁 tweet selection

Our community is looking for talented writers who are passionate about our ecosystem (Jamstack, open-source, javascript) and willing to share their knowledge/experiences through our Write for the community program.

Introduction

Note: The content of this tutorial was revised and updated on February 2, 2022. Some other information such as the title might have been updated later.

If you are familiar with our blog you must have seen that we've released a series of tutorials on how to make blogs using Strapi with a lot of frontend frameworks:

React is an open-source, front-end, JavaScript library for building user interfaces or UI components. Facebook and a community of individual developers and companies maintain it. React can be used as a base in the development of single-page or mobile applications.

Starters

You may want to try the result of this tutorial directly. Well, we made a starter out of it so give it a try!

Goal

The goal here is to be able to create a simple static blog website using Strapi as the backend and React as the frontend We will make this tutorial shorter and more efficient using our new templates.

Prerequisites

This tutorial will always use the latest version of Strapi. That is awesome, right!? You'll understand why below. You need to have node v.12 installed and that's all.

Setup

  • Create a blog-strapi folder and get inside!

take blog-strapi

Back-end setup

This is the easiest part of this tutorial, thanks to our expansion team who developed a series of Strapi templates that you can use for some different use cases.

These templates are Strapi applications containing existing collection types and single types suited for the appropriate use case and data. In this tutorial, we'll use the Blog template and connect a React application.

  • Create your Strapi backend folder using the Blog template.
# Using yarn
yarn create strapi-app backend --quickstart --template @strapi/template-blog@1.0.0 blog

# Using npm
npx create-strapi-app backend --quickstart --template @strapi/template-blog@1.0.0 blog

Don't forget that Strapi is running on http://localhost:1337. Create your admin user by signing up!

That's it! You're done with Strapi! I'm not kidding; we can now create our React application to fetch our content from Strapi.

Ok ok, wait, let's talk about this fantastic template you just created.

You should know that before we had starters and templates we only had tutorials. The idea of creating starters came to us when we realized that we could do something with the end result of our tutorials. Thus our starters were born. However, Strapi evolves quickly, very quickly. At the time, starters were made up of a repository that included the backend as well as the frontend. This meant that updating the Strapi version on all our starters took too much time. Therefore we decided to develop templates that are always created with the latest versions of Strapi. This was achieved simply by passing the repository parameter to the desired template like you just did. This method also gives a recommended architecture for your Strapi project.

These templates provide a solid basis for your Strapi application and include the following:

  • 3 Collection types are already created. Article, Category, Writer.
  • 2 Single types are already created. Global, Homepage.
  • Find and FindOne permissions are open for all your collection and single-types.
  • Mock data.

Feel free to modify any of this if you want. However, for this tutorial, this initial setup should be enough.

Nice! Now that Strapi is ready, you will create your React application.

Front-end setup

The easiest part has been completed; let's get our hands dirty developing our blog!

React setup

  • Create a React frontend server by running the following command:
# Using yarn
yarn create react-app frontend

# Using npm
npx create-react-app frontend

Once the installation is completed, you can start your front-end app to ensure everything went ok.

cd frontend
yarn dev

Front-end structure

You are going to give a better structure to your React application! But first, let's install react-router-dom

  • Install react-router-dom by running the following command:

yarn add react-router-dom

  • Remove everything inside your src folder

  • Create an index.js file containing the following code:

import React from "react";
import ReactDOM from "react-dom";
import App from "./containers/App";
import { BrowserRouter as Router } from "react-router-dom";

ReactDOM.render(
 <Router>
 <App />
 </Router>,
 document.getElementById("root")
);

What you are doing here is wrapping your App inside Router. But what/where is your App ?

Well, we're going to create a containers folder that will contain your App

  • Create a containers/App folder and a index.js file inside containing the following code:
import React from "react";

function App() {
 return <div className="App" />;
}

export default App;

You should have an empty working React app from now. To make your blog look pretty, we will use a popular CSS framework for styling: UiKit and Apollo to query Strapi with GraphQL.

UIkit setup

  • Add the following line in your public/index.html to use UIkit from their CDN
...
<title>React App</title>
<link
 rel="stylesheet"
 href="https://fonts.googleapis.com/css?family=Staatliches"
 />
 <link
 rel="stylesheet"
 href="https://cdn.jsdelivr.net/npm/uikit@3.2.3/dist/css/uikit.min.css"
 />

 <script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.2.0/js/uikit.min.js"></script>
 <script src="https://cdn.jsdelivr.net/npm/uikit@3.2.3/dist/js/uikit-icons.min.js"></script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.2.0/js/uikit.js"></script>
...
  • Create an ./src/index.css file containing the following style:
a {
 text-decoration: none;
}

h1 {
 font-family: Staatliches;
 font-size: 120px;
}

#category {
 font-family: Staatliches;
 font-weight: 500;
}

#title {
 letter-spacing: 0.4px;
 font-size: 22px;
 font-size: 1.375rem;
 line-height: 1.13636;
}

#banner {
 margin: 20px;
 height: 800px;
}

#editor {
 font-size: 16px;
 font-size: 1rem;
 line-height: 1.75;
}

.uk-navbar-container {
 background: #fff !important;
 font-family: Staatliches;
}

img:hover {
 opacity: 1;
 transition: opacity 0.25s cubic-bezier(0.39, 0.575, 0.565, 1);
}

Please don't force me to explain you some CSS!

Apollo setup

  • Install all the necessary dependencies for apollo by running the following command

yarn add apollo-boost @apollo/react-hooks graphql react-apollo

  • Create a ./src/utils/apolloClient.js file containing the following code:
import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { HttpLink } from "apollo-link-http";

const cache = new InMemoryCache();
const link = new HttpLink({
 uri: `${process.env.REACT_APP_BACKEND_URL}/graphql`
});
const client = new ApolloClient({
 cache,
 link
});

export default client;

Oh oh! It looks like you don't have any REACT_APP_BACKEND_URL environment variable yet!

  • Create a .env file in the root of your application containing the following line:

REACT_APP_BACKEND_URL=http://localhost:1337

You'll need to restart your server.

Note: You want Apollo to point to this address http://localhost:1337/graphql. That's the one where you'll be able to fetch your data from your Strapi server.

  • Wrap your App inside the ApolloProvider and import your index.css file in the ./src/index.js:
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter as Router } from "react-router-dom";

import { ApolloProvider } from "@apollo/react-hooks";

import App from "./containers/App";

import client from "./utils/apolloClient";

import "./index.css";

ReactDOM.render(
 <Router>
 <ApolloProvider client={client}>
 <App />
 </ApolloProvider>
 </Router>,
 document.getElementById("root")
);

Awesome! Your app should be ready now! You should have a blank page if everything went ok

Create the Query component

You are going to use Apollo to fetch your data from different pages. We don't want you to rewrite the same code every time on your pages. This is why you will write a Query component that will be reusable!

  • Create a ./src/components/Query/index.js file containing the following code:
import React from "react";
import { useQuery } from "@apollo/react-hooks";

const Query = ({ children, query, slug }) => {
 const { data, loading, error } = useQuery(query, {
 variables: { slug: slug }
 });

 if (loading) return <p>Loading...</p>;
 if (error) return <p>Error: {JSON.stringify(error)}</p>;
 return children({ data });
};

export default Query;

We use the useQuery hook to call your Strapi server at this address http://localhost:1337/graphql. We are sending an id if it exists (it will be necessary when you want to fetch just one article).

If the request is successful, you will return the child component with the retrieved data as prop.

Let's try it out by creating your navbar that will fetch all our categories but first, let's create the GraphQL query

  • Create a ./src/queries/category/categories.js file containing the following code:
import gql from "graphql-tag";

const CATEGORIES_QUERY = gql`
 query Categories {
 categories {
 data {
 attributes {
 slug
 name
 }
 }
 }
 }
`;

export default CATEGORIES_QUERY;

Let's use this query to display your categories inside your navbar

  • Create a ./src/components/Nav/index.js file containing the following code:
import React from "react";
import Query from "../Query";
import { Link } from "react-router-dom";

import CATEGORIES_QUERY from "../../queries/category/categories";

const Nav = () => {
 return (
 <div>
 <Query query={CATEGORIES_QUERY} id={null}>
 {({ data: { categories } }) => {
 return (
 <div>
 <nav className="uk-navbar-container" data-uk-navbar>
 <div className="uk-navbar-left">
 <ul className="uk-navbar-nav">
 <li>
 <Link to="/">Strapi Blog</Link>
 </li>
 </ul>
 </div>

 <div className="uk-navbar-right">
 <ul className="uk-navbar-nav">
 {categories.data.map((category) => {
 return (
 <li key={category.attributes.slug}>
 <Link
 to={`/category/${category.attributes.slug}`}
 className="uk-link-reset"
 >
 {category.attributes.name}
 </Link>
 </li>
 );
 })}
 </ul>
 </div>
 </nav>
 </div>
 );
 }}
 </Query>
 </div>
 );
};

export default Nav;

Since we want our Nav to be on every pages of our application we are going to use it inside our App container

  • Import and declare your Nav component inside your containers/App/index.js
import React from "react";
import Nav from "../../components/Nav";

function App() {
 return (
 <div className="App">
 <Nav />
 </div>
 );
}

export default App;

Great! You should now be able to see your brand new navbar containing your categories. But the links are not working right now. We'll work on this later in the tutorial, don't worry.

Note: The current code is unsuitable for displaying many categories as you may encounter a UI issue. Since this blog post is supposed to be short, you could improve the code by adding a lazy load.

Create the Articles containers

This container will wrap a component that will display all your articles.

  • Create a containers/Articles/index.js file containing the following code:
import React from "react";
import Articles from "../../components/Articles";
import Query from "../../components/Query";
import ARTICLES_QUERY from "../../queries/article/articles";

const Home = () => {
 return (
 <div>
 <div className="uk-section">
 <div className="uk-container uk-container-large">
 <h1>Strapi blog</h1>
 <Query query={ARTICLES_QUERY}>
 {({ data: { articles } }) => {
 return <Articles articles={articles.data} />;
 }}
 </Query>
 </div>
 </div>
 </div>
 );
};

export default Home;

Let's write the query that fetches all your articles.

  • Create a ./src/queries/article/articles.js file containing the following code:
import gql from "graphql-tag";

const ARTICLES_QUERY = gql`
 query Articles {
 articles {
 data {
 attributes {
 slug
 title
 category {
 data {
 attributes {
 slug
 name
 }
 }
 }
 image {
 data {
 attributes {
 url
 }
 }
 }
 }
 }
 }
 }
`;

export default ARTICLES_QUERY;

Now we need to create an Articles component that will display all of your articles and a Card component that will display each of your articles:

  • Create a components/Articles/index.js file containing the following:
import React from "react";
import Card from "../Card";

const Articles = ({ articles }) => {
 const leftArticlesCount = Math.ceil(articles.length / 5);
 const leftArticles = articles.slice(0, leftArticlesCount);
 const rightArticles = articles.slice(
 leftArticlesCount,
 articles.length
 );

 return (
 <div>
 <div className="uk-child-width-1-2" data-uk-grid>
 <div>
 {leftArticles.map((article) => {
 return (
 <Card
 article={article}
 key={`article__${article.attributes.slug}`}
 />
 );
 })}
 </div>
 <div>
 <div className="uk-child-width-1-2@m uk-grid-match" data-uk-grid>
 {rightArticles.map((article) => {
 return (
 <Card
 article={article}
 key={`article__${article.attributes.slug}`}
 />
 );
 })}
 </div>
 </div>
 </div>
 </div>
 );
};

export default Articles;
  • Create a components/Card/index.js file containing the following code:
import React from "react";
import { Link } from "react-router-dom";

const Card = ({ article }) => {
 const imageUrl =
 process.env.NODE_ENV !== "development"
 ? article.attributes.image.data.attributes.url
 : process.env.REACT_APP_BACKEND_URL +
 article.attributes.image.data.attributes.url;
 return (
 <Link to={`/article/${article.attributes.slug}`} className="uk-link-reset">
 <div className="uk-card uk-card-muted">
 <div className="uk-card-media-top">
 <img src={imageUrl} alt={article.attributes.image.url} height="100" />
 </div>
 <div className="uk-card-body">
 <p id="category" className="uk-text-uppercase">
 {article.attributes.category.data.attributes.name}
 </p>
 <p id="title" className="uk-text-large">
 {article.attributes.title}
 </p>
 </div>
 </div>
 </Link>
 );
};

export default Card;

Everything is ready, we just need to import the Articles container inside the App container.

  • Import and declare your Articles container inside your containers/App/index.js:
import React from "react";

import Articles from "../Articles";
import Nav from "../../components/Nav";

function App() {
 return (
 <div className="App">
 <Nav />
 <Articles />
 </div>
 );
}

export default App;

Create the Article container

You can see that if you click on the article, there is nothing. Let's create the article container together! But first, you'll need two packages:

  • Install react-moment and react-markdown by running the following command:

yarn add react-moment react-markdown moment

react-moment will allow you to display the publication date of your article, and react-markdown will be used to display the content of your article in markdown.

  • Create a ./containers/Article/index.js file containing the following:
import React from "react";
import { useParams } from "react-router";
import Query from "../../components/Query";
import ReactMarkdown from "react-markdown";
import Moment from "react-moment";

import ARTICLE_QUERY from "../../queries/article/article";

const Article = () => {
 let { slug } = useParams();

 return (
 <Query query={ARTICLE_QUERY} slug={slug}>
 {({ data: { articles } }) => {
 if (articles.data.length) {
 const imageUrl =
 process.env.NODE_ENV !== "development"
 ? articles.data[0].attributes.image.data.attributes.url
 : process.env.REACT_APP_BACKEND_URL +
 articles.data[0].attributes.image.data.attributes.url;

 return (
 <div>
 <div
 id="banner"
 className="uk-height-medium uk-flex uk-flex-center uk-flex-middle uk-background-cover uk-light uk-padding uk-margin"
 data-src={imageUrl}
 data-srcset={imageUrl}
 data-uk-img
 >
 <h1>{articles.data[0].attributes.title}</h1>
 </div>

 <div className="uk-section">
 <div className="uk-container uk-container-small">
 <ReactMarkdown source={articles.data[0].attributes.content} />
 <p>
 <Moment format="MMM Do YYYY">
 {articles.data[0].attributes.published_at}
 </Moment>
 </p>
 </div>
 </div>
 </div>
 );
 }
 }}
 </Query>
 );
};

export default Article;

Let's write the query for just one article now!

  • Create a ./src/queries/article/article.js containing the following code:
import gql from "graphql-tag";

const ARTICLE_QUERY = gql`
 query Article($slug: String!) {
 articles(filters: { slug: { eq: $slug } }) {
 data {
 attributes {
 slug
 title
 category {
 data {
 attributes {
 slug
 name
 }
 }
 }
 image {
 data {
 attributes {
 url
 }
 }
 }
 }
 }
 }
 }
`;

export default ARTICLE_QUERY;

The article page is ready, we just need to add this new Article container inside the App container. You are going to use the Switch and Route components from react-router-dom in order to establish a routing system for your article page

  • Import and declare your Article container inside your containers/App/index.js:
import React from "react";

import { Routes, Route } from "react-router-dom";

import Nav from "../../components/Nav";
import Articles from "../Articles";
import Article from "../Article";

function App() {
 return (
 <div className="App">
 <Nav />
 <Routes>
 <Route path="/" element={<Articles />} exact />
 <Route path="/article/:slug" element={<Article />} exact />
 </Routes>
 </div>
 );
}

export default App;

Great! You should be able to get your article now!

Categories

You may want to separate your article depending on the categories! Let's create a page for each category then:

  • Create a ./containers/Category/index.js file containing the following:
import React from "react";
import { useParams } from "react-router";
import Articles from "../../components/Articles";
import Query from "../../components/Query";
import CATEGORY_ARTICLES_QUERY from "../../queries/category/articles";

const Category = () => {
 let { slug } = useParams();

 return (
 <Query query={CATEGORY_ARTICLES_QUERY} slug={slug}>
 {({ data: { categories } }) => {
 if (categories.data.length) {
 return (
 <div>
 <div className="uk-section">
 <div className="uk-container uk-container-large">
 <h1>{categories.data[0].attributes.name}</h1>
 <Articles articles={categories.data[0].attributes.articles.data} />
 </div>
 </div>
 </div>
 );
 }
 }}
 </Query>
 );
};

export default Category;
  • Create a ./src/queries/category/articles.js file containing the following:
import gql from "graphql-tag";

const CATEGORY_ARTICLES_QUERY = gql`
 query Category($slug: String!) {
 categories(filters: { slug: { eq: $slug } }) {
 data {
 attributes {
 slug
 name
 articles {
 data {
 attributes {
 slug
 title
 content
 category {
 data {
 attributes {
 name
 }
 }
 }
 image {
 data {
 attributes {
 url
 }
 }
 }
 }
 }
 }
 }
 }
 }
 }
`;

export default CATEGORY_ARTICLES_QUERY;

Category page is ready, we just need to add this new container inside the App container.

  • Import and declare your Category container inside your containers/App/index.js:
import React from "react";

import { Routes, Route } from "react-router-dom";

import Nav from "../../components/Nav";
import Articles from "../Articles";
import Article from "../Article";
import Category from "../Category";

function App() {
 return (
 <div className="App">
 <Nav />
 <Routes>
 <Route path="/" element={<Articles />} exact />
 <Route path="/article/:slug" element={<Article />} exact />
 <Route path="/category/:slug" element={<Category />} exact />
 </Routes>
 </div>
 );
}

export default App;

Awesome! You can now list articles depending on the selected category.

Conclusion

Huge congrats, you successfully achieved this tutorial. I hope you enjoyed it!

Still hungry?

Feel free to add additional features, adapt this project to your needs, and give feedback in the comment section below. For example, the template used in this tutorial has an SEO component that is not used in the front end. Feel free to include it, but we advise using Gatsby or Next.js if you want to have an SEO-friendly blog.

Please note: Since we initially published this blog, we released new versions of Strapi, and tutorials may be outdated. Sorry for the inconvenience if it's the case, and please help us by reporting it here.

This article was updated on February 15th, 2021


Get started with Strapi by creating a project using a starter or trying our live demo. Also, consult our forum if you have any questions. We will be there to help you.

See you soon!
👁 tweet selection
Maxime Castres
Growth Engineer

Maxime started to code in 2015 and quickly joined the Growth team of Strapi. He particularly likes to create useful content for the awesome Strapi community. Send him a meme on Twitter to make his day: @MaxCastres