Develop a CRUD App in React Js With a Fake API Rest | by L Javier Tovar | Apr, 2022

Photograph by Safar Safarov on Unsplash

We frontend builders are confronted with a typical process, which is to simulate a REST service or backend REST API to ship some knowledge in JSON format to the frontend utility and ensure every thing works as anticipated.

It’s because many occasions the backend companies are usually not prepared when the frontend staff has to begin their growth. Due to time, we can’t wait, and we should begin displaying info to the consumer even whether it is simulated.

After all, we are able to arrange a whole backend server, for instance, utilizing Node.js, Categorical, and MongoDB. Nevertheless, as we stated earlier than, we don’t have time, and that is when the next query arises:

How one can notice CRUD performance with out creating your backend service?

On this tutorial, we’ll see arrange and use a pretend REST API server utilizing json-server. Which we are able to use whereas creating an online utility with react.

json-server supplies a full pretend REST API that requires no configuration or coding to arrange. With its assist, we are able to rapidly and simply arrange a backend for prototyping and mocking with out worrying in regards to the underlying logic.

This manner, we are able to focus extra on creating the frontend and fewer on configuring the backend.

Learn all about the json-server here.

Setting Up json-server

To begin utilizing json-server, we set up the package deal with the next command:

npm set up -g json-server

We create a JSON file referred to as db.js with some knowledge as your requirement wants. For instance, in my case, I would like some JSON knowledge with person info like id, title, username, and many others. Like the next:

"customers": [

"name": "Sobhan name",
"username": "Sobhan",
"email": "",
"phone": "9893548125",
"companiesId": "1",
"id": 1
"firms": [

"id": 1,
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net"

Now to get the server up from the terminal, run the next command:

json-server --watch db.json

With this, we should always have the server prepared at http://localhost:3000. Now we are able to make REST requests to this endpoint, as proven within the following picture:


The REST API that we’re going to create can be completely useful, that’s, it’ll carry out all attainable operations on our knowledge (studying, modifying, inserting, and deleting), storing the info within the JSON file.

The persistence of the info is completed within the JSON textual content file itself. On this approach, in future entry to the net utility, the API will bear in mind its state preserving all of the exercise that has been finished over time.

Setting Up React app

We create our utility with the next command:

npx create-react-app myApp

After that, we create the next construction for the mission:

├── api/
│ └── db.json
├── parts/
│ └── CrudUSer.js
│ └── DropCompanies.js
│ └── Type.js
│ └── Desk.jsx
├── helpers/
│ └── httpHelper.js
├── kinds/
│ └── App.css
├── App.js
└── index.js

Now that now we have created a react utility, we have to transfer our server to a different port because the react utility is already occupying port 3000.

We do that with the next command, and we’ll take the chance to create a customized command within the manifest file scripts.

"server": "json-server --watch src/api/db.json --port 5000"

Making requests to the pretend API

Earlier than making any request, we have to elevate our server, to do this, we execute the next command:

npm run server

GET request

Let’s begin by making a GET request to the REST URL. Inside the JSON file, now we have outlined a person’s endpoint that comprises info associated to the customers. By making a GET request to the URL http://localhost:5000/customers, it ought to show the present info.

For this tutorial, we add a relation of customers and firms every person belongs to an organization with json-server. We will make this question as follows:


POST request

So as to add new info to the present info, we’ll make a POST request to the URL http://localhost:5000/customers. Here’s what the POST request would appear like:

Attempt making a GET request, and you need to have the newly added info within the db.json file.


DELETE request

To delete an entry from the json-server knowledge, you must ship a DELETE request to the API endpoint with the person id. For instance, to delete the person with id 1, you must ship a DELETE request to the endpoint http://localhost:5000/customers/7.


As soon as it’s deleted, attempt to make a GET request, and the person with id 7 shouldn’t be within the returned JSON.

UPDATE request

To replace an present entry, you must ship a PATCH / PUT request with the small print that must be up to date for that individual entry. For instance, to replace the small print for the person with id 1, we’ll ship a PUT request to the URL http://localhost:5000/customers/1 as proven:


Creating an HTTP helper

I’ve created the helper helpers/httpHelper.js, with which we’ll make the completely different HTTP requests that we want. The concept is to have a mini-library that permits us to simplify the requests from react parts.

You can use this helper in any of your initiatives with any js framework or simply with vanilla js.

1 — The customFetch() perform. It’s an asynchronous perform that receives as parameters the URL to make the request and an choices object. If we don’t obtain the choices parameter, this can be an empty object, and we’ll assign the default values as the tactic and headers.

In any other case, we substitute or concatenate the acquired values with the default values.

As well as, now we have one other property referred to as AbortController which is able to enable us to abort the request after a sure time if the server doesn’t give us a response.

Lastly, we make the fetch request with the URL to seek the advice of and the generated choices, and we return the reply.

2 — Now that now we have a customized perform that permits us to make any sort of request, we’ll create a perform for every HTTP verb that we want, and we’ll ship it to name our customFetch() .

3 — Lastly, we export the capabilities that make the HTTP queries to have the ability to use them from our react parts.

Consuming the pretend API from react

To simply devour the pretend API, for this tutorial, I’ve created a element that can have all of the requests to make and their states of them and can share them with the opposite parts. You can adapt this element or create your individual relying on how your app works or add it to a worldwide state.

1 — We import and execute our helper httpHelper() .

2 — Create a perform for every request passing the mandatory parameters. These parameters can be obtained from the frontend, for instance, from the shape to create a person.

3 — We are going to go these capabilities and the states to the parts that want them, on this case, we solely have the <Type/> element that we use to create a person and to replace a person and the <Desk/> element that reveals us the record of customers.

You will discover these parts within the shared repository beneath, there may be not a lot to clarify about these parts since what we’re fascinated by is create and devour a REST API.

That’s it!, now we have our easy CRUD working, and it appears like this:

json-server CRUD with React

Venture repo:

On this tutorial, we realized make a backend, on this case, a REST API, rapidly out there for consumption. With this, we don’t want to attend for the backend staff to have the ability to begin our growth and begin displaying info in our utility.

We noticed use REST APIs with json-server to create a dummy database. We realized make requests so as to add, replace, modify and delete knowledge. We additionally created a helper to make HTTP requests that we are able to use in any mission.

More Posts