-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseDebounce.story.tsx
42 lines (38 loc) · 1.06 KB
/
useDebounce.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 { storiesOf } from '@storybook/react';
import * as React from 'react';
import { useDebounce } from '../src';
import ShowDocs from './util/ShowDocs';
const Demo = () => {
const [state, setState] = React.useState('Typing stopped');
const [val, setVal] = React.useState('');
const [debouncedValue, setDebouncedValue] = React.useState('');
const [, cancel] = useDebounce(
() => {
setState('Typing stopped');
setDebouncedValue(val);
},
2000,
[val]
);
return (
<div>
<input
type="text"
value={val}
placeholder="Debounced input"
onChange={({ currentTarget }) => {
setState('Waiting for typing to stop...');
setVal(currentTarget.value);
}}
/>
<div>{state}</div>
<div>
Debounced value: {debouncedValue}
<button onClick={cancel}>Cancel debounce</button>
</div>
</div>
);
};
storiesOf('Side effects/useDebounce', module)
.add('Docs', () => <ShowDocs md={require('../docs/useDebounce.md')} />)
.add('Demo', () => <Demo />);