While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the 'report an issue' button at the bottom of the tutorial.
- Fetch Post Example
- React Fetch Post Example
- React Fetch Post
- React Fetch Post Example
- React Fetch Post Example
- React Fetch Post Parameters
Feb 23, 2020 Using Fetch to Post Data. The Fetch API is not limited to GET requests only. You can make all other types of requests (POST, PUT, DELETE, etc.) with custom request headers and post data. Here is an example of a POST request. May 05, 2020 Learn, how to make a post request in react hooks using fetch API. Making a post request in React hooks. This below example sends an http post request to the json placeholder api using fetch where useState hook is used to store the user entered data. Learn, how to make a post request in react hooks using fetch API. Making a post request in React hooks. This below example sends an http post request to the json placeholder api using fetch where useState hook is used to store the user entered data. Hope this guide will help you fetch data and teach you how to use react hooks for other use cases as well. ? To access the full working demo, click this link and download it from the repo. The code in the repo is the same as it is in the article so you can follow up easier.
We all remember the dreaded XMLHttpRequest we used back in the day to make requests, it involved some really messy code, it didn’t give us promises and let’s just be honest, it wasn’t pretty JavaScript, right? Maybe if you were using jQuery, you used the cleaner syntax with
jQuery.ajax()
. Well JavaScript has it’s own built-in clean way now. Along comes the Fetch API a new standard to make server request jam-packed with promises and all those things we learned to love over the years.
How do we use the Fetch API?
In a very simple manner all you really do is call fetch with the URL you want, by default the Fetch API uses the GET method, so a very simple call would be like this:
Looks pretty simple right? So let’s starting using it… Iina 1 0 1 – modern video player youtube.
We are now going to build a simple GET request, in this case, I will use the Random User API and we will get 10 users and show them on the page using vanilla JavaScript.
Let’s get started with the HTML, all we really need is a heading and an unordered list:
The idea is to get all the data from the Random User API and display it in list items inside the author’s list.
The first step is to actually set the URL we need and also the list we are gonna put the data in, so in the Javascript we write:
I have set these to consts so you don’t risk changing these in the future and these two are meant to be constants through all the project.
Now we get into actual Fetch API:
Now we get into actual Fetch API:
Let’s review this code, shall we?
So first we are calling the Fetch API and passing it the URL we defined as a constant above and since no more parameters are set this is a simple GET request.
Then we get a response but the response we get is not JSON but an object with a series of methods we can use depending on what we want to do with the information, these methods include:
So first we are calling the Fetch API and passing it the URL we defined as a constant above and since no more parameters are set this is a simple GET request.
Then we get a response but the response we get is not JSON but an object with a series of methods we can use depending on what we want to do with the information, these methods include:
Fetch Post Example
![React fetch post form data React fetch post form data](https://minigolfmontreal.com/images/245853.png)
- clone() - As the method implies this method creates a clone of the response.
- redirect() - This method creates a new response but with a different URL.
- arrayBuffer() - In here we return a promise that resolves with an ArrayBuffer.
- formData() - Also returns a promise but one that resolves with FormData object.
- blob() - This is one resolves with a Blob.
- text() - In this case it resolves with a string.
- json() - Lastly we have the method to that resolves the promise with JSON.
Looking at all these methods the one we want is the JSON one because we want to handle our data as a JSON object so we add:
Now let’s get to the part we create the list items, for that, I created two helper functions at the top of my file just to make the code simpler down the line:
All these functions do is append and create elements as you can see.
Once this is done we can move on to the resolution of our promise and add the code we need to append these list items to our unordered list:
Once this is done we can move on to the resolution of our promise and add the code we need to append these list items to our unordered list:
So first we define authors as the response we get from the request then we map over all the authors and for each we create a list item, a span, and an image.
In the image source, we place the picture of the user, the HTML of the span will be the first and last name interpolated and then all we need to do is append this to their rightful parents and voilá, our HTTP request in vanilla JavaScript is done and returning something to the HTML.
In the image source, we place the picture of the user, the HTML of the span will be the first and last name interpolated and then all we need to do is append this to their rightful parents and voilá, our HTTP request in vanilla JavaScript is done and returning something to the HTML.
To handle our catch all I will do is console log the error as we get it but you can do whatever you want with the error such as append it to the HTML with the functions we created. This is the full code of our little request:
Handling more requests like POST
React Fetch Post Example
So this is a GET request, the default one for the fetch function but of course we can do all other types of requests and also change the headers and off course send data, all we need for this is to set our object and pass it as the second argument of the fetch function:
React Fetch Post
You can also define cache, mode and all those things you are used to defining in your POST requests.
React Fetch Post Example
To create our object and use the fetch function we also have another option and that is to use the request constructor to create our request object, so instead of defining the object in the function itself we do this:
React Fetch Post Example
You can use the way you are most comfortable with to build your request object.
React Fetch Post Parameters
While the Fetch API is not yet supported by all the browsers (currently Safari does not support it) it is the beautiful replacement for XMLHttpRequest we desperately needed in our life.
There are also polyfills if you really want to use it in more professional projects.
There are also polyfills if you really want to use it in more professional projects.