You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the last argument of `useFetch` is not a dependency array `[]`, then it will not fire until you call one of the http methods like `get`, `post`, etc.
93
+
92
94
```js
93
95
importuseFetchfrom'use-http'
94
96
@@ -131,18 +133,20 @@ function Todos() {
131
133
```
132
134
</details>
133
135
134
-
<details open><summary><b>Basic Usage (no managed state) <code>useFetch</code></b></summary>
135
-
136
+
<details><summary><b>Basic Usage (no managed state) <code>useFetch</code></b></summary>
137
+
138
+
This fetch is run `onMount/componentDidMount`. The last argument `[]` means it will run `onMount`. If you pass it a variable like `[someVariable]`, it will run `onMount` and again whenever `someVariable` changes values (aka `onUpdate`). **If no method is specified, GET is the default**
139
+
136
140
```js
137
141
importuseFetchfrom'use-http'
138
142
139
143
functionTodos() {
140
-
constoptions= { // accepts all `fetch` options
141
-
onMount:true, // will fire on componentDidMount (GET by default)
142
-
data: []// setting default for `data` as array instead of undefined
144
+
// accepts all `fetch` options
145
+
constoptions= {
146
+
data: [],// setting default for `data` as array instead of undefined
143
147
}
144
-
145
-
const { loading, error, data } =useFetch('https://example.com/todos', options)
148
+
149
+
const { loading, error, data } =useFetch('https://example.com/todos', options, []) // onMount (GET by default)
146
150
147
151
return (
148
152
<>
@@ -157,17 +161,16 @@ function Todos() {
157
161
```
158
162
</details>
159
163
160
-
<details open><summary><b>Basic Usage with <code>Provider</code></b></summary>
164
+
<details open><summary><b>Basic Usage (no managed state) with <code>Provider</code></b></summary>
The `onNewData` will take the current data, and the newly fetched data, and allow you to merge the two however you choose. In the example below, we are appending the new todos to the end of the current todos.
| `url` | Allows you to set a base path so relative paths can be used for each request :) | empty string |
604
-
| `onMount` | Once the component mounts, the http request will run immediately | `false` |
605
-
| `onUpdate` | This is essentially the same as the dependency array for useEffect. Whenever one of the variables in this array is updated, the http request will re-run. | `[]` |
642
+
| `onNewData` | Merges the current data with the incoming data. Great for pagination. | `(curr, new) =>new` |
606
643
| `onAbort` | Runs when the request is aborted. | empty function |
607
644
| `onTimeout` | Called when the request times out. | empty function |
608
645
| `retries` | When a request fails or times out, retry the request this many times. By default it will not retry. | `0` |
609
646
| `timeout` | The request will be aborted/cancelled after this amount of time. This is also the interval at which `retries` will be made at. **in milliseconds** | `30000` </br> (30 seconds) |
610
647
| `data` | Allows you to set a default value for `data` | `undefined` |
611
-
| `loading` | Allows you to set default value for `loading` | `false` unless `onMount ===true` |
648
+
| `loading` | Allows you to set default value for `loading` | `false` unless the last argument of `useFetch` is `[]` |
612
649
| `interceptors.request` | Allows you to do something before an http request is sent out. Useful for authentication if you need to refresh tokens a lot. | `undefined` |
613
650
| `interceptors.response` | Allows you to do something after an http response is recieved. Useful for something like camelCasing the keys of the response. | `undefined` |
614
651
615
652
```jsx
616
-
useFetch({
653
+
constoptions={
617
654
// accepts all `fetch` options such as headers, method, etc.
618
-
url:'https://example.com', // used to be `baseUrl`
619
-
onMount:true,
620
-
onUpdate: [] // everytime a variable in this array is updated, it will re-run the request (GET by default)
621
-
onTimeout: () => {}, // called when the request times out
622
-
onAbort: () => {}, // called when aborting the request
623
-
retries:3, // amount of times it should retry before erroring out
624
-
timeout:10000, // amount of time before the request (or request(s) for retries) errors out.
625
-
data: [], // default for `data` field
626
-
loading:false, // default for `loading` field
627
-
interceptors: { // typically, `interceptors` would be added as an option to the `<Provider />`
655
+
656
+
// used to be `baseUrl`. You can set your URL this way instead of as the 1st argument
657
+
url:'https://example.com',
658
+
659
+
// called when the request times out
660
+
onTimeout: () => {},
661
+
662
+
// called when aborting the request
663
+
onAbort: () => {},
664
+
665
+
// this will allow you to merge the data however you choose. Used for Pagination
666
+
onNewData: (currData, newData) => {
667
+
return [...currData, ...newData]
668
+
},
669
+
670
+
// amount of times it should retry before erroring out
671
+
retries:3,
672
+
673
+
// amount of time before the request (or request(s) for each retry) errors out.
674
+
timeout:10000,
675
+
676
+
// set's the default for the `data` field
677
+
data: [],
678
+
679
+
// set's the default for `loading` field
680
+
loading:false,
681
+
682
+
// typically, `interceptors` would be added as an option to the `<Provider />`
683
+
interceptors: {
628
684
request:async (options) => { // `async` is not required
629
685
return options // returning the `options` is important
630
686
},
631
687
response: (response) => {
632
688
return response // returning the `response` is important
0 commit comments