04 Feb, 2024 - About 9 minutes
Strapi and React TODO Application
Intro
In this article I will build a Todo App with Strapi for the backend component and React as frontend. The guide was originally written by Chigozie Oduah check the references links as he as some very interesting articles about Strapi.
What is Strapi
The leading open-source headless CMS. 100% JavaScript / TypeScript and fully customizable.
Setup backend with Strapi
I will be using bun to setup packages due to improved performance checkout their page if you want to know more.
Let’s start by creating our backend with the command
bunx create-strapi-app todo-list --quickstart |
This should have created a new folder todo-list
you can run the following command on that folder to start your development
You should now access the browser to http://localhost:1337/admin and create you admin account so that we can start create a new collection.
If you need to restart the development environment you can enter the todo-list folder and run
bun run develop |
Building the Backend
Now for our TODO application lets create a collection.
- Navigate to Content-Type Builder
- Select Create new collection type
- Call it Todo
Strapi uses this name to reference this collection within our application. Strapi automatically uses the display name to fill the rest of the text boxes.
Create the following fields:
- item : Type ( Text )
And hit Save, as our application will be a simple Todo list application that single field will do the job.
Add test entries
After the collection is created, we add some test entries.
- Go to content Manager
- select the Todo collection and choose Create New entry
- After filling the item information you can Save and Publish
Repeat the previous step to have more entries.
Create API Endpoint for our collection
We create API endpoints for our frontend using the Todo collection. These endpoints allows a frontend to interact with our collection.
- Navigate to Settings
- Click on Roles under user permission & roles.
- Click on public to open the permissions given to the public.
- Toggle the Todo dropdown under Permissions and Select all to allow public access to our collection without auth.
- Hit Save
After performing the following steps you should be able to access the API
You should have a working APIs
- Find (
/api/todos GET
): We use this endpoint to get all the items in our Todo collection - Create (
/api/todos POST
): We use this endpoint to create a new item in our to-do collection. - Find one (
/api/todos/ GET
): We use this endpoint to get an item in our Todo collection. - Update (
/api/todos/ PUT
): We use this endpoint to update an item in our Todo collection - Delete (
/api/todos/ DELETE
): We use this endpoint to delete an item in our Todo collection.
Great that was easy, now lets setup our frontend React application to interact with this API endpoints.
Setup frontend React App
Now lets start the frontend application on the parent folder run the following command
bunx create-react-app todo-frontend |
Next create the following two files for the environment variables:
- .env.development
REACT_APP_BACKEND=http://localhost:1337/ |
- .env.production
REACT_APP_BACKEND=/ |
You can run the frontend application with the following command
bun run start |
And access the browser at http://localhost:3000 which will hold an empty react application.
Lets replace the App.js
file the following content
import { useState, useEffect } from 'react'; |
Create the following file TodoItem.jsx
with the following content:
import { useState } from "react"; |
Also replace App.css file with the following content:
.app { |
After the last update you should have a workable todo app http://localhost:3000/
Deployment
I’ve seen several articles where developers bundle the frontend application on the public folder to keep a single server installation, but according to Strapi is not a good practice.
Conclusion
In this article we have setup Strapi to setup the backend for a Todo list application and a react frontend that would take advantage of the provided APIs using a headless architecture.
Strapi allows to quickly setup APIs for Collections that can be defined and managed through a provided UI. Very useful if one would like to decouple the development process, or if you don’ t won’ t to implement from scratch backend functionalities.
Regarding the level of customization would require extensive exploration. The backoffice allows to create auth tokens, webhooks, SSO, internationalization and also has a marketplace area to include more functionalities.
Also worth mention that if you can leverage Strapi Cloud to deploy your Production applications