@ahmetelgun/usefetch

Fetch hook for React

Usage no npm install needed!

<script type="module">
  import ahmetelgunUsefetch from 'https://cdn.skypack.dev/@ahmetelgun/usefetch';
</script>

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.