useThrottle()
npm i @react-hook/throttle
A React hook for throttling setState and other callbacks.
Quick Start
import {useThrottle, useThrottleCallback} from '@react-hook/throttle'
const Component = props => {
const [value, setValue] = useThrottle('initialValue')
}
const useMyCallback = (initialState, wait, leading) => {
const [state, setState] = useState(initialState)
return [state, useThrottleCallback(setState, wait, leading)]
}
API
useThrottle(initialState: any, fps?: number, leading?: boolean)
Options
Property | Type | Default | Description |
---|
initialState | any | | The initial state stored in useState |
fps | number | 30 | Defines the rate in frames per second with which setState is invoked with new state |
leading | boolean | false | Calls setState on the leading edge (right away). When false , setState will not be called until the next frame is due |
Returns [state, setState]
Variable | Type | Description |
---|
state | any | The value set by setState or the initialState |
setState | Function | A throttled setState callback |
useThrottleCallback(callback: Function, wait?: number, leading?: boolean)
Options
Property | Type | Default | Description |
---|
callback | Function | | This is the callback you want to throttle |
fps | number | 30 | Defines the rate in frames per second with which setState is invoked with new state |
leading | boolean | false | Calls setState on the leading edge (right away). When false , setState will not be called until the next frame is due |
Returns throttledCallback
Variable | Type | Description |
---|
throttledCallback | Function | A throttled version of your callback |
LICENSE
MIT