I need to send GET and POST requests to many endpoints.
For example, GET | POST Fruit List, GET | POST Weather List...
Not on one page, they are divided(Browser Route). ex) Fruit Page, Weather Page...
I'd like to know how to make a custom react hook in this situation.
I searched a lot, but all the writing was based on a single api endpoint.
Way 1
Create a reusable fetch function.
function useGetFetch(url){
const response = await fetch(url, ...
return response.json()
}
function usePostFetch(url){
const response = await fetch(url, ...
return response.json()
}
Way 2
Create function one for each.
function getFruitList(){
const response = await fetch('/fruit', ...
}
function postFruitList(){
const response = await fetch('/fruit', ...
}
function getWeatherList(){
const response = await fetch('/weather', ...
}
function postWeatherList(){
const response = await fetch('/weather', ...
}