useFetch()
useFetch( STRING endpoint[, STRING method = "GET", OBJECT body ])
This uses useAsync()
, so it acts similarly.
Parameters
endpoint
(STRING, Required, default: none) This, obviously, has to be a URL.method
(STRING, Optional, default: "GET") Should be one of these.body
(OBJECT, Optional, default: empty) If method is "GET", this is converted to a query string. Otherwise, it's passed in the body.
Returns
(OBJECT) { execute, status, error, value: {
headers, ok, status, statusText, json, text
} }
headers
(HEADERS)ok
(BOOLEAN) from Responsestatus
(INTEGER) from ResponsestatusText
(STRING) from Responsejson
(OBJECT or FALSE)text
(STRING or FALSE)
Examples
import { useFetch } from "@preemptivelove/utilities-js"const { ok, statusText, json } = useFetch( "https://swapi.dev/api/people/1/" )if ( ok )doSomethingWith( json )elseexamine( statusText )
import { useFetch } from "@preemptivelove/utilities-js"const { ok, statusText, json } = useFetch("https://swapi.dev/api/people/","POST", {name: "Yo Mama",appearedIn: "nothing"} )if ( ok )doSomethingWith( json )elseexamine( statusText )
Source
https://github.com/PreemptiveLove/utilities-js/blob/master/src/hooks/useFetch.js