Logo

dev-resources.site

for different kinds of informations.

Debounce Input in React

Published at
2/22/2023
Categories
debounce
react
performance
javascript
Author
manishkc104
Author
11 person written this
manishkc104
open
Debounce Input in React

Debouncing an input is a technique used to improve web application performance and user experience. When a user types into an input field, the application may perform several operations, such as filtering a list, fetching data from an API, or performing other processes based on the user's input. These operations can be computationally expensive and slow down the application or even cause it to crash if performed too frequently.

Debouncing an input in React involves setting a delay between when a user types into an input and when the input's value is updated.

To create a debounce input in react you can use the following steps.

Solution 1

First we use the useState hook provided by React to store the input variable in a state.

const [inputValue, setInputValue] = React.useState("")
Enter fullscreen mode Exit fullscreen mode

Then we create a function called handleInputChange which will handle the input changes and then update the input value with setInputValue

const handleInputChange = (event) => {
   setInputValue(event.target.value);
}
Enter fullscreen mode Exit fullscreen mode

Moving forward we again use the useState hook provided by React to store the debounced input value

const [debouncedInputValue, setDebouncedInputValue] = React.useState("")
Enter fullscreen mode Exit fullscreen mode

Now we use the useEffect hook and perform a delay before we update the debouncedInputValue.

React.useEffect(() => {
  const delayInputTimeoutId = setTimeout(() => {
    setDebouncedInputValue(inputValue);
  }, 500);
  return () => clearTimeout(delayInputTimeoutId);
}, [inputValue, 500]);
Enter fullscreen mode Exit fullscreen mode

500 milliseconds is used as the delay time to update the deboucedInputValue. We can add the time according to our requirements.

The useEffect will run every time the inputValue changes, after which the delay of 500 milliseconds will happen, and then the deboucneInputValue get updated with the inputValue

Now we can use the debounceInputValue while calling the API or wherever needed. Here the full solution

import React from "react";

const DebounceInput = () => {
  const [inputValue, setInputValue] = React.useState("");
  const [debouncedInputValue, setDebouncedInputValue] = React.useState("");

  const handleInputChange = (event) => {
    setInputValue(event.target.value);
  };

  React.useEffect(() => {
    const timeoutId = setTimeout(() => {
      setDebouncedValue(inputValue);
    }, 500);
    return () => clearTimeout(timeoutId);
  }, [inputValue, 500]);

  return <input type="text" value={inputValue} onChange={handleInputChange} />;
};

Enter fullscreen mode Exit fullscreen mode

Solution 2

For this solution we will be using the debounce function from use-debounce

Firstly we will need to install lodash in our application by running the following command

npm install use-debounce
Enter fullscreen mode Exit fullscreen mode

Then we import the debounce function from use-debounce in out React component

import { useDebounce } from "use-debounce";
Enter fullscreen mode Exit fullscreen mode

After the import is done state is declared for storing the input value

const [inputValue, setInputValue] = React.useState("");
Enter fullscreen mode Exit fullscreen mode

Then we create a function called handleInputChange which will handle the input changes and then update the input value with setInputValue

const handleInputChange = (event) => {
   setInputValue(event.target.value);
}
Enter fullscreen mode Exit fullscreen mode

Then we will use the useDebounce hook to debounce the input value. The first argument of useDebounce takes the input value, and the second argument takes the time for the delay. Then the hook will return debounced value which is debouncedValue.

const [debouncedValue] = useDebounce(inputValue, 500);
Enter fullscreen mode Exit fullscreen mode

Now we can use the debouncedValue wherever necessary.Here is the full solution

import React from "react";
import { useDebounce } from "use-debounce";
const DebounceInput = () => {
  const [inputValue, setInputValue] = React.useState("");
  const [debouncedValue] = useDebounce(inputValue, 500);

  const handleInputChange = (event) => {
    const value = event.target.value;
    setInputValue(value);
  };

  return <input type="text" value={inputValue} onChange={handleInputChange} />;
};
Enter fullscreen mode Exit fullscreen mode
debounce Article's
30 articles in total
Favicon
If you think debounce or onblur is not working in React, you can check it.
Favicon
Enhance Your React Native App with Debouncing for API Optimization
Favicon
Dboun
Favicon
React: Debouncing input with useEffect
Favicon
Debounce Method for Searching
Favicon
Optimizing React Performance with useDebounce.
Favicon
Understanding JavaScript Debounce and Throttling
Favicon
Debounce function with a return value using promises
Favicon
Introducing dwayne/elm-debouncer: Debounce or throttle your actions in Elm
Favicon
Debouncing De-coded
Favicon
Some Problems I Face When Building React Single Page Application And How I Solve Them
Favicon
Debounce Input in React
Favicon
Como aplicar debounce em input de busca sem lodash
Favicon
Debouncing: how to use it
Favicon
Debounce in Next.js
Favicon
Optimized React Search Bar 🔯
Favicon
Learn Debounce and Throttle
Favicon
Используем Throttle и Debounce в React
Favicon
Debouncing in Javascript using a custom function or Lodash library.
Favicon
Debounce Function in Javascript 🚀
Favicon
Debouncing TypeScript
Favicon
How to use Lodash debounce method?
Favicon
Debounce in JavaScript
Favicon
What is Debouncing? Search Suggestions - A Javascript Implementation
Favicon
Throttle and Debounce in React
Favicon
Debounce and Throttle
Favicon
useDebounce hook in React, Improve performance of your React app with Debouncing
Favicon
Debouncing (lodash) with React
Favicon
Use lodash.debounce inside a function component in React
Favicon
How to Debounce UIButton Taps

Featured ones: