@practicaljs/react-debounce
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

Practical debounce

Why

Many debounce examples and packages out there contain dependencies on larger packages like lodash. While lodash has many features you don't need the extra bloat in your application.

Hook

In the example below debounce will execute your callback after a 250ms delay.

import useDebounce from "@practicaljs/react-debounce";

const { debounce } = useDebounce();

const myMethod = () => {
	debounce(() => {
		someDelayedMethodHere("test");
	}, 250);
};

If you need control over canceling the debounce, use the cancel method.

import useDebounce from "@practicaljs/react-debounce";

const { cancel } = useDebounce();

useEffect(() => {
	return () => {
		cancel();
	};
});

Component

In the example below the DebounceInput component will call the onChange method after 250ms.

import DebouncedInput from "@practicaljs/react-debounce";

const listen = (event: ChangeEvent<HTMLInputElement>) => {
	console.log(event.target.value);
};

return <DebouncedInput delay={250} onChange={listen}></DebouncedInput>;

Readme

Keywords

Package Sidebar

Install

npm i @practicaljs/react-debounce

Weekly Downloads

0

Version

1.0.3

License

none

Unpacked Size

41.5 kB

Total Files

7

Last publish

Collaborators

  • rcanfield
  • harlenalvarez