Assume we all know about the debounce
function from lodash
.
If a user quickly input 1
,12
,
Try this (using useCallback):
import React, { useState, useCallback } from "react";
import "./styles.css";
import { debounce } from "lodash";
const request = debounce(value => {
alert(`request: ${value}`);
}, 1000);
export default function App() {
const [input, setInput] = useState("");
const debouceRequest = useCallback(value => request(value), []);
const onChange = e => {
debouceRequest(e.target.value);
setInput(e.target.value); // Remove this line will lead to normal denounce
};
return (
<div className="App">
<input onChange={onChange} />
</div>
);
}