-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseAsyncRetry.story.tsx
43 lines (40 loc) · 1.19 KB
/
useAsyncRetry.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
43
import { number, withKnobs } from '@storybook/addon-knobs';
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import { useAsyncRetry } from '../src';
import ShowDocs from './util/ShowDocs';
const Demo = ({ delay }) => {
const state = useAsyncRetry<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>
)}
<button onClick={() => state.retry()}>Retry</button>
<pre>{JSON.stringify(state, null, 2)}</pre>
</div>
);
};
storiesOf('Side effects/useAsyncRetry', module)
.addDecorator(withKnobs)
.add('Docs', () => <ShowDocs md={require('../docs/useAsyncRetry.md')} />)
.add('Demo', () => {
const delay = number('delay', 1000, { range: true, min: 100, max: 5000, step: 100 });
return <Demo delay={delay} />;
});