I have a problem about debounce in Function Component ReactJS.
Code here:
let timeout;
const App = () => {
const [value, setValue] = useState("");
const func = () => {
console.log("value: ", value);
};
const debounce = (func, wait) => {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => {
func();
}, wait);
};
const onChange = event => {
const { value: v } = event.target;
setValue(v);
debounce(func, 3000);
};
return (
<>
<Input
value={value}
name="value"
onChange={onChange}
placeholder="Basic usage"
/>
</>
);
};
Why when I press "123" then result of debounce function is: "12"