react-use-debouncer
A simple debounced state hook that I use on some of my projects.
Install with npm:
npm i --save react-use-debouncer
or via yarn:
yarn i react-use-debouncer
Features
- Debounced state
- Cancelable debounced callback function
Docs
useDebouncedState
const state setState = ;
- initialValue: The initial value that state is initialized with
- delay(optional): Number indicating the milliseconds to wait before setting state
Returns an array containing the debounced state and a setState function.
ex: [state, setState]
useDebouncedCallback
const debouncedCallback cancelCallback = ;
- callback: Callback function to be debounced
- delay(optional): Number indicating the milliseconds to wait before calling the function
Returns an array containing the debounced callback function and a cancel function
ex: [debouncedCallback, cancelCallback]
Usage
useDebouncedState
; const useStateExample = { const state setState = ; return <div className="App"> <span>state</span> <input onChange= /> </div> ;};
Or check it out on CodeSandbox
useDebouncedCallback
; const sampleFunction = console; const useCallbackExample = { const debouncedCallback cancelCallback = ; return <button onClick= >Click Me!</button>;};
Future
- Leading & trailing options
- Better CodeSandBox examples
- Better docs