Bring debounce & throttle of lodash version to react-hooks
npm install use-lodash-debounce-throttle
Bring debounce & throttle of lodash version to react-hooks.
useDebounce & useThrottle will auto create, update and cancel debounced & throttled instance, so you do not need to cancel manually.
shell
yarn add use-lodash-debounce-throttle
`$3
`jsx
import { useDebounce } from 'use-lodash-debounce-throttle';const Com = () => {
const debouncedChange = useDebounce((v) => {
postUpdate();
}, 500, { maxWait: 2000 });
return (
type="text"
onChange={(e) => {
debouncedChange(e.target.value)
}}
/>
)
}
`$3
`jsx
import { useThrottle } from 'use-lodash-debounce-throttle';const Com = () => {
const debouncedChange = useThrottle((v) => {
sendMessage();
}, 500, { leading: false });
return (
type="text"
onChange={(e) => {
debouncedChange(e.target.value)
}}
/>
)
}
`API
- useDebounce: same as https://lodash.com/docs/4.17.11#debounce
- useThrottle: same as https://lodash.com/docs/4.17.11#throttleDEV
`
yarn install
yarn start
``