Skip to content

Commit 8a4ca61

Browse files
committed
Improve README:
Transform Usage to Api. Add example to top of readme.
1 parent 37311d0 commit 8a4ca61

File tree

1 file changed

+73
-38
lines changed

1 file changed

+73
-38
lines changed

README.md

Lines changed: 73 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,7 @@
44
[![npm version](https://img.shields.io/npm/v/react-fetch-hook.svg)](https://www.npmjs.com/package/react-fetch-hook)
55
[![npm downloads](https://img.shields.io/npm/dt/react-fetch-hook.svg)](https://www.npmjs.com/package/react-fetch-hook)
66

7-
React hook, which allows you to conveniently work with *fetch*. Good Flow support.
8-
9-
## Installation
10-
11-
Install it with yarn:
12-
13-
```
14-
yarn add react-fetch-hook
15-
```
16-
17-
Or with npm:
18-
19-
```
20-
npm i react-fetch-hook --save
21-
```
22-
23-
## Usage
24-
25-
*useFetch* hook accepts the same arguments as *fetch* function.
7+
React hook for conveniently use Fetch API.
268

279
```javascript
2810
import React from "react";
@@ -40,27 +22,64 @@ const Component = () => {
4022

4123
```
4224

43-
You can pass any *fetch* options:
25+
*useFetch* accepts the same arguments as *fetch* function.
26+
27+
## Installation
28+
29+
Install it with yarn:
30+
31+
```
32+
yarn add react-fetch-hook
33+
```
34+
35+
Or with npm:
36+
37+
```
38+
npm i react-fetch-hook --save
39+
```
40+
41+
## API
42+
43+
### `useFetch`
44+
Create a hook wrapper for `fetch` call.
4445
```javascript
45-
const { isLoading, data } = useFetch("https://swapi.co/api/people/1", {
46-
method: "get",
47-
headers: {
48-
Accept: "application/json, application/xml, text/plain, text/html, *.*",
49-
"Content-Type": "application/json; charset=utf-8"
46+
useFetch(
47+
path: RequestInfo,
48+
options?: {
49+
...RequestOptions,
50+
formatter?: Response => Promise
51+
depends?: Array<boolean>
52+
},
53+
specialOptions?: {
54+
depends?: Array<boolean>
5055
}
51-
});
52-
56+
): TUseFetchResult
5357
```
54-
### Custom formatter
55-
You can pass *formatter* prop for using custom formatter function. Default is used *response => response.json()* formatter.
58+
where `TUseFetchResult` is:
59+
```javascript
60+
type TUseFetchResult<T> = {
61+
data: any,
62+
isLoading: boolean,
63+
error: any
64+
}
65+
```
66+
#### Options:
67+
##### RequestInfo, RequestOptions
68+
`RequestInfo`, `RequestOptions` is [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) args.
69+
70+
##### formatter
71+
`formatter` - optional formatter function.
72+
Default is `response => response.json()` formatter.
73+
Example:
5674
```javascript
5775
const { isLoading, data } = useFetch("https://swapi.co/api/people/1", {
5876
formatter: (response) => response.text()
5977
});
6078

6179
```
62-
### Prevent call `fetch`
63-
For prevent call fetch you can pass *depends* prop:
80+
81+
##### depends
82+
The request will not be called until all elements of `depends` array be truthy. Example:
6483

6584
```javascript
6685
const {authToken} = useContext(authTokenContext);
@@ -70,14 +89,30 @@ const { isLoading, data } = useFetch("https://swapi.co/api/people/1", {
7089
});
7190

7291
```
73-
Motivation: you can apply hooks only at top level of function.
74-
Calling hooks inside `if` or `for` statements, or change count of hooks may throw React error.
92+
93+
If any element of `depends` changed, request will be re-call. For example, you can use [react-use-trigger](https://github.com/ilyalesik/react-use-trigger) for re-call the request:
7594
```javascript
76-
// Potential сrash, because may call different count of hooks:
77-
const {authToken} = useContext(authTokenContext);
78-
if (!authToken) {
79-
return null;
95+
import {createTrigger, useTrigger} from "react-use-trigger";
96+
97+
const requestTrigger = createTrigger();
98+
99+
export const Subscriber = () => {
100+
const requestTriggerValue = useTrigger(requestTrigger);
101+
102+
const { isLoading, data } = useFetch("https://swapi.co/api/people/1", {
103+
depends: [requestTriggerValue]
104+
});
105+
106+
return <div />;
80107
}
81-
const { isLoading, data } = useFetch("https://swapi.co/api/people/1");
108+
109+
export const Sender = () => {
110+
return <button onClick={() => {
111+
requestTrigger() // re-call request
112+
}}>Send</button>
113+
}
114+
115+
82116

83117
```
118+

0 commit comments

Comments
 (0)