-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# `useDebounce` | ||
|
||
React hook that delays invoking a function until after wait milliseconds have elapsed since the last time the debounced function was invoked. | ||
|
||
The third argument is the array of values that the debounce depends on, in the same manner as useEffect. The debounce timeout will start when one of the values changes. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import React, { useState } from 'react'; | ||
import { useDebounce } from 'react-use'; | ||
|
||
const Demo = () => { | ||
const [state, setState] = React.useState('Typing stopped'); | ||
const [val, setVal] = React.useState(''); | ||
|
||
useDebounce( | ||
() => { | ||
setState('Typing stopped'); | ||
}, | ||
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> | ||
); | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```ts | ||
useDebouce(fn, ms: number, args: any[]); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { useDebounce } from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const [state, setState] = React.useState('Typing stopped'); | ||
const [val, setVal] = React.useState(''); | ||
|
||
useDebounce( | ||
() => { | ||
setState('Typing stopped'); | ||
}, | ||
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> | ||
); | ||
}; | ||
|
||
storiesOf('Side effects/useDebounce', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useDebounce.md')} />) | ||
.add('Demo', () => <Demo />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
const useDebounce = (fn: () => any, ms: number = 0, args: Array<any> = []) => { | ||
const [timeout, setTimeoutVar] = useState<number>(-1); | ||
|
||
useEffect(() => { | ||
// if args change then clear timeout | ||
clearTimeout(timeout); | ||
const t = setTimeout(fn.bind(null, args), ms); | ||
setTimeoutVar(t); | ||
}, args); | ||
}; | ||
|
||
export default useDebounce; |