-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseAsync.story.tsx
42 lines (39 loc) · 1.11 KB
/
useAsync.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
38
39
40
41
42
import { number, withKnobs } from '@storybook/addon-knobs';
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import { useAsync } from '../src';
import ShowDocs from './util/ShowDocs';
const Demo = ({ delay }) => {
const state = useAsync<string>(
() =>
new Promise<string>((resolve, reject) => {
setTimeout(() => {
if (Math.random() > 0.5) {
resolve('✌️');
} else {
reject(new Error('A pseudo random error occurred'));
}
}, delay);
}),
[delay]
);
return (
<div>
{state.loading ? (
<p>Loading...</p>
) : state.error ? (
<p>Error: {state.error.message}</p>
) : (
<p>Value: {state.value}</p>
)}
<pre>{JSON.stringify(state, null, 2)}</pre>
</div>
);
};
storiesOf('Side effects/useAsync', module)
.addDecorator(withKnobs)
.add('Docs', () => <ShowDocs md={require('../docs/useAsync.md')} />)
.add('Demo', () => {
const delay = number('delay', 1000, { range: true, min: 100, max: 5000, step: 100 });
return <Demo delay={delay} />;
});