Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@react-hook/throttle

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-hook/throttle

A React hook for throttling setState and other callbacks

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
202K
decreased by-7.13%
Maintainers
1
Weekly downloads
 
Created
Source

useThrottle()

Bundlephobia Types NPM Version MIT License

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) => {
  // 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?)

export const useThrottle = <State>(
  initialState: State | (() => State),
  fps?: number,
  leading?: boolean
): [State, Dispatch<SetStateAction<State>>] => {
  const [state, setState] = useState<State>(initialState)
  return [state, useThrottleCallback(setState, fps, leading)]
}
Options
PropertyTypeDefaultDescription
initialStateanyThe initial state stored in useState
fpsnumber30Defines the rate in frames per second with which setState is invoked with new state
leadingbooleanfalseCalls setState on the leading edge (right away). When false, setState will not be called until the next frame is due
Returns [state, setState]
VariableTypeDescription
stateanyThe value set by setState or the initialState
setStateDispatch<SetStateAction<State>>A throttled setState callback

useThrottleCallback(callback, fps?, leading?)

export const useThrottleCallback = <CallbackArgs extends any[]>(
  callback: (...args: CallbackArgs) => any,
  fps = 30,
  leading = false
): ((...args: CallbackArgs) => void)
Options
PropertyTypeDefaultDescription
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.
fpsnumber30Defines the rate in frames per second with which setState is invoked with new state
leadingbooleanfalseCalls setState on the leading edge (right away). When false, setState will not be called until the next frame is due
Returns throttledCallback
VariableTypeDescription
throttledCallback((...args: CallbackArgs) => void)A throttled version of your callback

LICENSE

MIT

Keywords

FAQs

Package last updated on 23 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc