@ahmetelgun/usefetch
v0.0.6
Published
Fetch hook for React
Downloads
5
Readme
useFetch
Fetch hook for React
Installation
npm install @ahmetelgun/usefetch
Usage
import useFetch from "@ahmetelgun/usefetch";
.
.
const [response, loading, error] = useFetch("url");
if(loading){
return <div>loading</div>;
}
else if(error){
return <div>error</div>;
}
else if (response){
//use response
}
{ //response schema
status: 200,
data: {
//data
}
}
import useFetch from "@ahmetelgun/usefetch";
.
.
const [response, loading, error, callFetch] = useFetch();
if(expression){
callFetch("url")
}
if(another_expression){
callFetch("url")
//if you call callFetch function before the old request ends, old request is aborted.
}
import useFetch from "@ahmetelgun/usefetch";
.
.
const options = {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
}
}
const [response, loading, error, callFetch] = useFetch("url", options);
.
.
callFetch("url", options /*or different options*/)
//data, loading and error are reset every time you call callFetch function.