-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: new hook `useAsync` * test: tests for `useAsync` hook * docs: docs for `useAsync` hook
- Loading branch information
Showing
7 changed files
with
607 additions
and
0 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,30 @@ | ||
import * as React from 'react'; | ||
import { useAsync } from '../..'; | ||
|
||
export const Example: React.FC = () => { | ||
const [state, actions] = useAsync( | ||
() => | ||
new Promise<string>((resolve) => { | ||
setTimeout(() => { | ||
resolve('react-hookz is awesome!'); | ||
}, 3000); | ||
}), | ||
[] | ||
); | ||
|
||
return ( | ||
<div> | ||
<div> | ||
<em>Async function will resolve after 3 seconds of wait</em> | ||
</div> | ||
<br /> | ||
<div>promise status: {state.status}</div> | ||
<div>current value: {state.result ?? 'undefined'}</div> | ||
<br /> | ||
<div> | ||
<button onClick={actions.reset}>reset</button>{' '} | ||
<button onClick={actions.execute}>execute</button> | ||
</div> | ||
</div> | ||
); | ||
}; |
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,57 @@ | ||
import { Canvas, Meta, Story } from '@storybook/addon-docs/blocks'; | ||
import { Example } from './example.stories'; | ||
|
||
<Meta title="Side-effect/useAsync" component={Example} /> | ||
|
||
# useAsync | ||
|
||
Executes provided async function and tracks its result and error. | ||
|
||
- Handles any async functions. | ||
- Safe - no worries about updating the state of unmounted component. | ||
- Stable - returned methods do not change between renders. | ||
- Handles race conditions - only latest results are stored in state. | ||
- SSR-friendly, no requests performed on the server. | ||
- Provides methods to manually trigger fetch. | ||
- Options to customize behaviour. | ||
|
||
#### Example | ||
|
||
<Canvas> | ||
<Story story={Example} inline /> | ||
</Canvas> | ||
|
||
## Reference | ||
|
||
```ts | ||
export function useAsync<Result, Args extends unknown[] = unknown[]>( | ||
asyncFn: (...params: Args) => Promise<Result>, | ||
args: Args, | ||
options?: IUseAsyncOptions<Result> | ||
): [IAsyncState<Result>, IUseAsyncActions<Result, Args>, IUseAsyncMeta<Result, Args>]; | ||
``` | ||
|
||
#### Arguments | ||
|
||
- **asyncFn** _`(...params: Args) => Promise<Result>`_ - Function that returns a promise. | ||
- **args** _`Args`_ - Arguments list that will be passed to async function. Acts as dependencies | ||
list for underlying `useEffect` hook. | ||
- **options** - Hook options: | ||
- **initialValue** _`Result`_ _(default: `undefined`)_ - Value that will be set on initialisation, | ||
before the async function is executed. | ||
- **skipMount** _`boolean`_ _(default: `false`)_ - Skip mount async function execution. | ||
- **skipUpdate** _`boolean`_ _(default: `false`)_ - Skip update async function execution. | ||
|
||
#### Return | ||
|
||
0. **state** | ||
- **status** _`'loading' | 'success' | 'error' | 'not-executed'`_ - latest promise status. | ||
- **result** _`Result | undefined`_ - promise result if promise fulfilled. | ||
- **error** _`Error | undefined`_ - promise error if promise rejected. | ||
1. **methods** | ||
- **reset** _`() => void`_- Reset state to initial, when async function haven't been executed. | ||
- **execute** _`(...args: Args) => Promise<Result>`_- Execute async function manually. | ||
2. **meta** | ||
- **promise** _`Promise<Result> | undefined`_- Recent promise returned from async function. | ||
- **lastArgs** _`Args | undefined`_ - List of arguments applied to recent async function | ||
invocation. |
Oops, something went wrong.