Skip to content

Commit 1ed2be7

Browse files
author
Alex Cory
committed
adding codepen examples to readme
1 parent 3d0cfca commit 1ed2be7

File tree

4 files changed

+83
-9
lines changed

4 files changed

+83
-9
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Usage
9191
<li><a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/usefetch-suspense-i22wv'>useFetch + Suspense</a></li>
9292
<li><a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/usefetch-provider-pagination-exttg'>useFetch + Pagination + Provider</a></li>
9393
<li><a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/usefetch-provider-requestresponse-interceptors-s1lex'>useFetch + Request/Response Interceptors + Provider</a></li>
94+
<li><a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/usefetch-retryon-retrydelay-s74q9'>useFetch + retryOn, retryDelay</a></li>
9495
<li><a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/graphql-usequery-provider-uhdmj'>useQuery - GraphQL</a></li>
9596
</ul>
9697

@@ -754,6 +755,44 @@ const App = () => {
754755
755756
</details>
756757
758+
<details><summary><b>retryOn & retryDelay</b></summary>
759+
760+
In this example you can see how `retryOn` will retry on a status code of `305`, or if we choose the `retryOn()` function, it returns a boolean to decide if we will retry. With `retryDelay` we can either have a fixed delay, or a dynamic one by using `retryDelay()`.
761+
762+
```js
763+
import useFetch from 'use-http'
764+
765+
const TestRetry = () => {
766+
const { response, get } = useFetch('https://httpbin.org/status/305', {
767+
retryOn: [305]
768+
// OR
769+
retryOn: ({ attempt, error, response }) => {
770+
// returns true or false to determine whether to retry
771+
return error || response && response.status >= 300
772+
},
773+
774+
retryDelay: 3000,
775+
// OR
776+
retryDelay: ({ attempt, error, response }) => {
777+
// exponential backoff
778+
return Math.min(attempt > 1 ? 2 ** attempt * 1000 : 1000, 30 * 1000)
779+
// linear backoff
780+
return attempt * 1000
781+
}
782+
})
783+
784+
return (
785+
<>
786+
<button onClick={() => get()}>CLICK</button>
787+
<pre>{JSON.stringify(response, null, 2)}</pre>
788+
</>
789+
)
790+
}
791+
```
792+
793+
[![Edit Basic Example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/usefetch-retryon-retrydelay-s74q9)
794+
795+
</details>
757796
758797
Overview
759798
--------

docs/README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Examples
7272
- <a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/usefetch-suspense-i22wv'>useFetch + Suspense</a>
7373
- <a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/usefetch-provider-pagination-exttg'>useFetch + Pagination + Provider</a>
7474
- <a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/usefetch-provider-requestresponse-interceptors-s1lex'>useFetch + Request/Response Interceptors + Provider</a>
75+
- <a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/usefetch-retryon-retrydelay-s74q9'>useFetch + retryOn, retryDelay</a>
7576
- <a target="_blank" rel="noopener noreferrer" href='https://codesandbox.io/s/graphql-usequery-provider-uhdmj'>useQuery - GraphQL</a>
7677

7778
Installation
@@ -575,6 +576,44 @@ const App = () => {
575576
}
576577
```
577578
579+
Retries
580+
-------
581+
582+
In this example you can see how `retryOn` will retry on a status code of `305`, or if we choose the `retryOn()` function, it returns a boolean to decide if we will retry. With `retryDelay` we can either have a fixed delay, or a dynamic one by using `retryDelay()`.
583+
584+
```js
585+
import useFetch from 'use-http'
586+
587+
const TestRetry = () => {
588+
const { response, get } = useFetch('https://httpbin.org/status/305', {
589+
retryOn: [305]
590+
// OR
591+
retryOn: ({ attempt, error, response }) => {
592+
// returns true or false to determine whether to retry
593+
return error || response && response.status >= 300
594+
},
595+
596+
retryDelay: 3000,
597+
// OR
598+
retryDelay: ({ attempt, error, response }) => {
599+
// exponential backoff
600+
return Math.min(attempt > 1 ? 2 ** attempt * 1000 : 1000, 30 * 1000)
601+
// linear backoff
602+
return attempt * 1000
603+
}
604+
})
605+
606+
return (
607+
<>
608+
<button onClick={() => get()}>CLICK</button>
609+
<pre>{JSON.stringify(response, null, 2)}</pre>
610+
</>
611+
)
612+
}
613+
```
614+
615+
[![Edit Basic Example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/usefetch-retryon-retrydelay-s74q9)
616+
578617
GraphQL Query
579618
---------------
580619
@@ -721,8 +760,8 @@ function App() {
721760
Hooks
722761
=======
723762
724-
| Option | Description |
725-
| --------------------- | ---------------------------------------------------------------------------------------- |
763+
| Option | Description |
764+
| --------------------- | ------------------ |
726765
| `useFetch` | The base hook |
727766
| `useQuery` | For making a GraphQL query |
728767
| `useMutation` | For making a GraphQL mutation |

src/__tests__/useFetch.test.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,7 @@ describe('useFetch - BROWSER - Overwrite Global Options set in Provider', (): vo
628628
() => useFetch(),
629629
{ wrapper }
630630
)
631-
await act(async () => {
632-
await result.current.get()
633-
})
631+
await act(result.current.get)
634632
expect(fetch.mock.calls[0][0]).toBe('https://example.com')
635633
expect((fetch.mock.calls[0][1] as any).headers).toEqual(expectedHeaders)
636634
expect(fetch).toHaveBeenCalledTimes(1)
@@ -801,9 +799,7 @@ describe('useFetch - BROWSER - retryOn & retryDelay', (): void => {
801799
await waitForNextUpdate()
802800
expect(result.current.error).toEqual({ name: 400, message: 'Bad Request' })
803801
expect(fetch.mock.calls.length).toBe(3)
804-
await act(async () => {
805-
await result.current.get()
806-
})
802+
await act(result.current.get)
807803
expect(result.current.error).toEqual({ name: 400, message: 'Bad Request' })
808804
expect(fetch.mock.calls.length).toBe(6)
809805
})

src/useFetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function useFetch<TData = any>(...args: UseFetchArgs): UseFetch<TData> {
167167
if (!(Number.isInteger(delay) && delay >= 0)) {
168168
console.error('retryDelay must be a number >= 0! If you\'re using it as a function, it must also return a number >= 0.')
169169
}
170-
attempt.current++
170+
++attempt.current
171171
if (delay) await sleep(delay)
172172
const d = await doFetch(...args)
173173
return d

0 commit comments

Comments
 (0)