-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
126 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# `useAsyncRetry` | ||
|
||
Uses `useAsync` with an additional `retry` method to easily retry/refresh the async function; | ||
|
||
|
||
## Usage | ||
|
||
```jsx | ||
import {useAsyncRetry} from 'react-use'; | ||
|
||
// Returns a Promise that resolves after one second. | ||
const fn = () => new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
if (Math.random() > 0.5) { | ||
reject(new Error('Random error!')); | ||
} else { | ||
resolve('RESOLVED'); | ||
} | ||
}, 1000); | ||
}); | ||
|
||
const Demo = () => { | ||
const state = useAsync(fn); | ||
|
||
return ( | ||
<div> | ||
{state.loading? | ||
<div>Loading...</div> | ||
: state.error? | ||
<div>Error...</div> | ||
: <div>Value: {state.value}</div> | ||
} | ||
{!state.loading? | ||
<a href='javascript:void 0' onClick={() => state.retry()}>Retry</a> | ||
: null | ||
} | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
|
||
## Reference | ||
|
||
```ts | ||
useAsyncRetry(fn, args?: any[]); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as React from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
import {useAsyncRetry} from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const fnRetry = () => | ||
new Promise<string>((resolve, reject) => { | ||
setTimeout(() => { | ||
if (Math.random() > 0.5) { | ||
reject(new Error('Random error!')); | ||
} else { | ||
resolve('RESOLVED'); | ||
} | ||
}, 1000); | ||
}); | ||
|
||
const DemoRetry = () => { | ||
const { loading, value, error, retry } = useAsyncRetry<string>(fnRetry); | ||
|
||
return ( | ||
<div> | ||
{loading? | ||
<div>Loading...</div> | ||
: error? | ||
<div>Error: {error.message}</div> | ||
: <div>Value: {value}</div> | ||
} | ||
<a href='javascript:void 0' onClick={() => retry()}>Retry</a> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('Side effects|useAsyncRetry', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useAsyncRetry.md')} />) | ||
.add('Demo', () => <DemoRetry />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useCallback, useState } from 'react'; | ||
import useAsync from './useAsync'; | ||
|
||
const useAsyncRetry = <T>(fn: () => Promise<T>, args: any[] = []) => { | ||
const [attempt, setAttempt] = useState<number>(0); | ||
const memoized = useCallback(() => fn(), [...args, attempt]); | ||
const state = useAsync(memoized); | ||
|
||
const retry = useCallback(() => { | ||
if (state.loading) { | ||
if (process.env.NODE_ENV === 'development') { | ||
console.log('You are calling useAsyncRetry hook retry() method while loading in progress, this is a no-op.'); | ||
} | ||
return; | ||
} | ||
setAttempt(attempt + 1); | ||
}, [memoized, state, attempt]); | ||
|
||
return { ...state, retry }; | ||
}; | ||
|
||
export default useAsyncRetry; |