Rooks

useDebouncedValue

About

Tracks another value and gets updated in a debounced way.

Examples

import React, { useState } from "react";
import { useDebouncedValue } from "rooks";
 
export default function App() {
  const [value, setValue] = useState("");
  const [debouncedValue, immediatelyUpdateDebouncedValue] = useDebouncedValue(
    value,
    500
  );
  // use `immediatelyUpdateDebouncedValue` if you want to update `debouncedValue` immediately
 
  return (
    <div>
      <input
        onChange={e => setValue(e.target.value)}
        placeholder="Please type here"
      />
      <div>{debouncedValue}</div>
    </div>
  );
}

Arguments

ArgumentTypeDescriptionDefault value
valueDatethe value to be debouncedundefined
timeoutnumbermilliseconds that it takes count down once1000
options.initializeWithNullbooleanShould the debouncedValue start off as null in the first renderfalse

Return Value

An array is returned with the following items in it

NameTypeDescription
debouncedValuetypeof valueThe debouncedValue
immediatelyUpdateDebouncedValuefunctionHandy utility function to update the debouncedValue instantly

On this page