1.2.1 • Published 4 years ago

react-use-debouncer v1.2.1

Weekly downloads
49
License
MIT
Repository
github
Last release
4 years ago

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] = useDebouncedState(initialValue, delay);
  • 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] = useDebouncedCallback(callback, delay);
  • 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

import { useDebouncedState } from 'react-use-debouncer';

const useStateExample = () => {
  const [state, setState] = useDebouncedState('Initial');

  return (
    <div className="App">
      <span>{state}</span>
      <input onChange={({ target: { value } }) => setState(value)} />
    </div>
  );
};

Or check it out on CodeSandbox

useDebouncedCallback

import { useDebouncedCallback } from 'react-use-debouncer';

const sampleFunction = () => console.log('There should be a delay before I appear!');

const useCallbackExample = () => {
  const [debouncedCallback, cancelCallback] = useDebouncedCallback(sampleFunction);

  return <button onClick={() => debouncedCallback()}>Click Me!</button>;
};

Future

  • Leading & trailing options
  • Better CodeSandBox examples
  • Better docs
1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.1.10

4 years ago

0.1.8

4 years ago

0.1.9

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago