-
-
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
7 changed files
with
127 additions
and
2 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
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,49 @@ | ||
# `useThrottle` | ||
|
||
React hook that invokes a function and then delays subsequent function calls until after wait milliseconds have elapsed since the last time the throttled function was invoked. | ||
|
||
The third argument is the array of values that the throttle depends on, in the same manner as useEffect. The throttle timeout will start when one of the values changes. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import React, { useState } from 'react'; | ||
import { useThrottle } from 'react-use'; | ||
|
||
const Demo = () => { | ||
const [status, setStatus] = React.useState('Updating stopped'); | ||
const [value, setValue] = React.useState(''); | ||
const [throttledValue, setThrottledValue] = React.useState(''); | ||
|
||
useThrottle( | ||
() => { | ||
setStatus('Waiting for input...'); | ||
setThrottledValue(value); | ||
}, | ||
2000, | ||
[value] | ||
); | ||
|
||
return ( | ||
<div> | ||
<input | ||
type="text" | ||
value={value} | ||
placeholder="Throttled input" | ||
onChange={({ currentTarget }) => { | ||
setStatus('Updating stopped'); | ||
setValue(currentTarget.value); | ||
}} | ||
/> | ||
<div>{status}</div> | ||
<div>Throttled value: {throttledValue}</div> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```ts | ||
useThrottle(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
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,39 @@ | ||
import * as React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { useThrottle } from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const [status, setStatus] = React.useState('Updating stopped'); | ||
const [value, setValue] = React.useState(''); | ||
const [throttledValue, setThrottledValue] = React.useState(''); | ||
|
||
useThrottle( | ||
() => { | ||
setStatus('Waiting for input...'); | ||
setThrottledValue(value); | ||
}, | ||
2000, | ||
[value] | ||
); | ||
|
||
return ( | ||
<div> | ||
<input | ||
type="text" | ||
value={value} | ||
placeholder="Throttled input" | ||
onChange={({ currentTarget }) => { | ||
setStatus('Updating stopped'); | ||
setValue(currentTarget.value); | ||
}} | ||
/> | ||
<div>{status}</div> | ||
<div>Throttled value: {throttledValue}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('Side effects|useThrottle', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useThrottle.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,28 @@ | ||
import { useRef, useEffect } from 'react'; | ||
|
||
const useThrottle = (fn: () => any, ms: number = 0, args?) => { | ||
const lastRan = useRef(0); | ||
|
||
useEffect(() => { | ||
let timeout | ||
const diff = Date.now() - lastRan.current | ||
|
||
if (diff >= ms) { | ||
fn.apply(null, args); | ||
lastRan.current = Date.now(); | ||
} else { | ||
timeout = setTimeout(() => { | ||
fn.apply(null, args); | ||
lastRan.current = Date.now(); | ||
}, ms - diff) | ||
} | ||
|
||
return () => { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
} | ||
} | ||
}, args); | ||
}; | ||
|
||
export default useThrottle; |