HTTP PATCH Method using React.js
Before we discuss what the PATCH method is let’s briefly what CRUD is. CRUD stands for Create, read, update and delete.
To create a new item to our database on the server whenever the user either submits a form or adds a new item to the page, we would most commonly use the POST method.
To read resources in a REST environment, or in other words display a list of items from the server when the application first renders, we would use the GET method.
To update a specific piece of data from the database or the entire database, we would use the PATCH or PUT method. A PUT method would be used if the entire database needs to be updated while the PATCH method would be used if we’re doing a partial update.
Lastly, to delete or remove data from the server we should use the DELETE method.
The PATCH method is very similar to the PUT method because it also modifies an existing resource. The difference is that for the PUT method, the request body contains the complete new version of the data, whereas, for the PATCH method, the request body only needs to contain the specific changes.
Get Ashley Mattam’s stories in your inbox
Join Medium for free to get updates from this writer.
Here’s an example of the PATCH method I used on a project I built for my Bootcamp (that’s still in progress). I created an app called FabBeauty that essentially helps users find the right product themselves based on reviews, likes, etc. When the application first renders you see a display of makeup items along with the name of the product and brand, as well as the price and number of likes each product has. I used the PATCH request so that the user is able to increase the number of likes.
Here is the code I wrote to make this happen::
After creating an initial state and pulling the data from the JSON file using the GET method, I used the map method to return a new array of the same length as the original array. In that same function where we are mapping, we want to make sure that we are only updating the likes for a “specific” product. We don’t want to update the likes of all of the products in our database. We want to make sure that it only updates if it matches the proper “id” of the product we’re updating.
I then pass the functions and the original data to the component where I want to create the PATCH method.
After we define all the data that I want to be displayed, I created a handleLikes function that will hold my PATCH request. Before fetching the data for my request, I created a variable that explains to the server that I want to increase my “likes” object by 1. Then, I fetched my data, and just like with a POST request, we need to specify the method, headers, and body options for our PATCH request as well. We also need to include the item’s ID in the URL so that our server knows which item we’re trying to update. Lastly, I passed the function down to the likes button and created an OnClick event listener so that every time the user clicks the button the function renders.








