forked from streamich/react-use
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseAsyncFn.story.tsx
37 lines (34 loc) · 983 Bytes
/
useAsyncFn.story.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import { useAsyncFn } from '../src';
import ShowDocs from './util/ShowDocs';
const Demo = () => {
const [state, callback] = useAsyncFn<string>(
() =>
new Promise<string>((resolve, reject) => {
setTimeout(() => {
if (Math.random() > 0.5) {
resolve('✌️');
} else {
reject(new Error('A pseudo random error occurred'));
}
}, 1000);
})
);
return (
<div>
{state.loading ? (
<p>Loading...</p>
) : state.error ? (
<p>Error: {state.error.message}</p>
) : (
<p>Value: {state.value}</p>
)}
<button onClick={() => callback()}>Start</button>
<pre>{JSON.stringify(state, null, 2)}</pre>
</div>
);
};
storiesOf('Side effects/useAsyncFn', module)
.add('Docs', () => <ShowDocs md={require('../docs/useAsyncFn.md')} />)
.add('Demo', () => <Demo />);