@ahmetelgun/usefetch

0.0.6 • Public • Published

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.

Readme

Keywords

Package Sidebar

Install

npm i @ahmetelgun/usefetch

Weekly Downloads

0

Version

0.0.6

License

MIT

Unpacked Size

4.1 kB

Total Files

4

Last publish

Collaborators

  • ahmetelgun