Debounce input
- Example of input debounce from Tanstack table: https://github.com/TanStack/table/blob/main/examples/react/filters/src/main.tsx
- https://stackoverflow.com/a/79325757
Best and easy to understand debounce
Live Demo
Code: DebounceInputDemo4useRef.jsx
// This is amazing.// https://stackoverflow.com/a/79682810import React, { useState, useRef } from 'react';export default function DebounceInputDemo4useRef() {const [inputValue, setInputValue] = useState('');const [debounceValue, setDebouncedValue] = useState('');const timerId = useRef();const handleChange = (event) => {const serchItem = event.target.value;setInputValue(serchItem);const delay = 1000;console.log("clear", timerId.current);clearTimeout(timerId.current);timerId.current = setTimeout(() => {console.log(timerId.current, serchItem); // necessary api callssetDebouncedValue(serchItem);}, delay);console.log("CREATED", timerId.current);};return (<><input type="text" value={inputValue} onChange={handleChange} placeholder='Type in quickly'/><div>{debounceValue}</div></>);};