2.2.0 • Published 5 years ago
@react-hook/throttle v2.2.0
A React hook for performantly throttling setState and other callbacks.
Quick Start
import {useThrottle, useThrottleCallback} from '@react-hook/throttle'
const Component = (props) => {
// at a basic level, used just like useState
const [value, setValue] = useThrottle('initialValue')
}
const useMyCallback = (initialState, wait, leading) => {
// this is the same code useThrottle() uses to throttle setState
const [state, setState] = useState(initialState)
return [state, useThrottleCallback(setState, wait, leading)]
}API
useThrottle(initialState, fps?, leading?)
A hook that acts just like React.useState, but with a setState function
that is only invoked at most X frames per second. A trailing call is guaranteed,
but you may opt-in to calling on the leading edge, as well.
export const useThrottle = <State>(
initialState: State | (() => State),
fps?: number,
leading?: boolean
): [State, Dispatch<SetStateAction<State>>]Arguments
| Property | Type | Default | Description |
|---|---|---|---|
| initialState | State | (() => State) | The initial state provided to React.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 | State | The current value in state |
| setState | Dispatch<SetStateAction<State>> | A throttled setState callback |
useThrottleCallback(callback, fps?, leading?)
A hook that invokes its callback at most X frames per second. A trailing call is guaranteed, but you may opt-in to calling on the leading edge, as well.
export const useThrottleCallback = <Callback extends (...args: any[]) => void>(
callback: Callback,
fps = 30,
leading = false
): CallbackArguments
| Property | Type | Default | Description |
|---|---|---|---|
| callback | ((...args: CallbackArgs) => void) | This is the callback you want to throttle. You need to wrap closures/unstable callbacks in useCallback() so that they are stable, otherwise throttling will break between renders. | |
| 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 | ((...args: CallbackArgs) => void) | A throttled version of your callback |
LICENSE
MIT
2.2.0
5 years ago
2.1.4
6 years ago
2.1.3
6 years ago
2.1.2
6 years ago
2.1.1
6 years ago
2.0.2
6 years ago
2.1.0
6 years ago
2.0.1
6 years ago
2.0.0
6 years ago
1.0.12
6 years ago
1.0.10
6 years ago
1.0.9
6 years ago
1.0.8
6 years ago
1.0.7
6 years ago
1.0.6
6 years ago
1.0.5
6 years ago
1.0.4
6 years ago
1.0.3
6 years ago
1.0.2
6 years ago
1.0.1
6 years ago
1.0.0
7 years ago
0.1.6
7 years ago
0.1.5
7 years ago
0.1.4
7 years ago
0.1.3
7 years ago
0.1.2
7 years ago
0.1.1
7 years ago
0.1.0
7 years ago