0.1.1 • Published 5 years ago

@tianrang-sz/react-hooks v0.1.1

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

react hooks

useDebounce react hook

Install it with yarn:

yarn add @tianrang-sz/react-hooks

Or with npm:

npm i @tianrang-sz/react-hooks --save

simple demo

import { useDebounce } from '@tianrang-sz/react-hooks';

function Input({ defaultValue }) {
  const [value, setValue] = useState(defaultValue);
  // Debounce callback
  const [debouncedCallback] = useDebounce(
    // function
    value => {
      setValue(value);
    },
    // delay in ms
    1000
  );

  // you should use `e => debouncedCallback(e.target.value)` as react works with synthetic evens
  return (
    <div>
      <input
        defaultValue={defaultValue}
        onChange={e => debouncedCallback(e.target.value)}
      />
      <p>Debounced value: {value}</p>
    </div>
  );
}
0.1.1

5 years ago

0.1.0

5 years ago