- ## useFetch
**1. GET**
```
import React from "react";
import { useFetch } from "use-utils";
function App() {
// GET METHOD EXAMPLE
const { data, isLoading, error } = useFetch(
"https://jsonplaceholder.typicode.com/posts/1"
);
return (
GET
getIsLoading: {isLoading ? "Loading ...." : null}
getError: {error ? error : null}
{JSON.stringify(data)}
);
}
```
**2. POST**
```
import React from "react";
import { useFetch } from "use-utils";
function App() {
// POST METHOD EXAMPLE
const { data, isLoading, error } = useFetch(
"https://jsonplaceholder.typicode.com/posts",
"POST",
{
title: "foo",
body: "bar",
userId: 1
},
{
"Content-type": "application/json; charset=UTF-8"
}
);
return (
POST
getIsLoading: {isLoading ? "Loading ...." : null}
getError: {error ? error : null}
{JSON.stringify(data)}
);
}
```
**3. PUT**
```
import React from "react";
import { useFetch } from "use-utils";
function App() {
// PUT METHOD EXAMPLE
const { data, isLoading, error } = useFetch(
"https://jsonplaceholder.typicode.com/posts/1",
"PUT",
{
id: 1,
title: "foo",
body: "bar",
userId: 1
},
{
"Content-type": "application/json; charset=UTF-8"
}
);
return (
PUT
getIsLoading: {isLoading ? "Loading ...." : null}
getError: {error ? error : null}
{JSON.stringify(data)}
);
}
```
#### signature
```
useFetch(url, method, payload, headers) -> retuns { data, isLoading, error }
arguments:
url: required
method: optional (default: 'GET')
payload: optional (default: null)
method: optional (default: null)
```
#### note
```
Don't use useFetch inside any function or inside any hook -> because react hooks relies on order of hook function. so, every hook needs to immediate function of react compoenent function (due to closure is required !)
```