-
-
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.
feat: 🎸 add useThrottleFn hook that throttles function
This hook used to be called useThrottle, but it also was re-implemented.
- Loading branch information
Showing
5 changed files
with
84 additions
and
4 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,40 @@ | ||
import * as React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { useThrottleFn, useCounter } from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const [value, setValue] = React.useState(''); | ||
const throttledValue = useThrottleFn(value => value, 2000, [value]); | ||
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|useThrottleFn', 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,36 @@ | ||
import {useState, useRef, useEffect} from 'react'; | ||
import useUnmount from './useUnmount' | ||
|
||
const useThrottleFn = <T>(fn: (...args: any[]) => T, ms: number = 200, args: any[]) => { | ||
const [state, setState] = useState<T>(null as any); | ||
let timeout = useRef<any>(null); | ||
const nextArgs = useRef(null) as any; | ||
const hasNextArgs = useRef(false) as any; | ||
|
||
useEffect(() => { | ||
if (!timeout.current) { | ||
setState(fn(...args)); | ||
const timeoutCallback = () => { | ||
if (hasNextArgs.current) { | ||
hasNextArgs.current = false; | ||
setState(fn(...nextArgs.current)); | ||
timeout.current = setTimeout(timeoutCallback, ms); | ||
} else { | ||
timeout.current = null; | ||
} | ||
}; | ||
timeout.current = setTimeout(timeoutCallback, ms); | ||
} else { | ||
nextArgs.current = args; | ||
hasNextArgs.current = true; | ||
} | ||
}, args); | ||
|
||
useUnmount(() => { | ||
clearTimeout(timeout.current); | ||
}); | ||
|
||
return state; | ||
}; | ||
|
||
export default useThrottleFn; |