React custom hooks examples for fetch post api

We are looking for a React custom hooks examples for the plain fetch post api.
With async await javascript.

When’s looking for one on Google we saw a lot with promises and Axios
We did not see any with fetch post api
a link to tutorial would be appreciated
or if someone can post on this form an example would be appreciated.

Thank you for your help.

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

Thank you for your kind guidance.

I was actually looking for an example for a fetch API custom React hook.

If what you need is an example so this is my code

I am working on a Fetch API where I sent a post request and I am supposed to get back a JSON with some information in it.

The code goes in to the

“if (result.ok)”

But the following happens to the code that happens afterwards

  1. When I do

console.log(responseText) I get an empty value.

  1. When I try

Console.log(JSON.stringify(result))

I get an empty array.

You can find my code at the following link:

What am I doing wrong?

The example you posted isn’t very useful to us. Without a working example and without knowing the backend code we can’t really say much.

But, I have a question. Is it actually returning text or JSON? What happens if you do?

await result.json();

Did you check the network tab in the browser dev tools?


Is the function you posted even a custom hook?

https://www.google.com/search?q=react+fetch+custom+hook

Thank you for pointing me to the right direction.

After checking the network tab I realized the issue was with the backend.

I fixed the backend and it works like a charm.

It is not a custom hook

I was hoping if you can send an article showing how to make a Fetch api with a custom hook

the link you sent me is using a classic promise not an async await

The issues with the links you sent me are:

1)The first is using a classic promise not an async await

2)The second link the issue is that it is using axios not fetch Fetch API.

You should read up on promises and async/await. Rewriting a fetch promise chain to async/await is fairly simple.

Rewriting Axios code to fetch is also simple as they are almost identical in their basic usage. One of the main basic differences is Axios returns JSON by default without needing to use .json()

If you are unable to rewrite either of the two to use fetch with async/await a little homework is in order.


There are literally tons of examples you can find by searching for it, just look at the results in the Google search I gave you. Here is one of the first results.

One improvement that might be made to that example is using an abort controller instead of a boolean flag.

https://www.google.com/search?q=react+fetch+custom+hook+abort+controller

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.