forked from streamich/react-use
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseThrottle.story.tsx
40 lines (36 loc) · 1.12 KB
/
useThrottle.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
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import { useCounter, useThrottle } from '../src';
import ShowDocs from './util/ShowDocs';
const Demo = () => {
const [value, setValue] = React.useState('');
const throttledValue = useThrottle(value, 2000);
const [lastThrottledValue, setLastThrottledValue] = React.useState(throttledValue);
const [count, { inc }] = useCounter();
React.useEffect(() => {
if (lastThrottledValue !== throttledValue) {
setLastThrottledValue(throttledValue);
inc();
}
});
return (
<div style={{ width: 300, margin: '40px auto' }}>
<input
type="text"
value={value}
placeholder="Throttled input"
style={{ width: '100%' }}
onChange={({ currentTarget }) => {
setValue(currentTarget.value);
}}
/>
<br />
<br />
<div>Throttled value: {throttledValue}</div>
<div>Times updated: {count}</div>
</div>
);
};
storiesOf('Side effects|useThrottle', module)
.add('Docs', () => <ShowDocs md={require('../docs/useThrottle.md')} />)
.add('Demo', () => <Demo />);