I’ve a textarea and I’d like to udpate text area text instantly as user enter text but I don’t like to hit api after every keystroke for obvious reason for efficiency.
I’ve tried to use debounce function but function is getting triggered as many times after every keystroke but with some delay that I’ve added in debounce
export default function App() {
const [text, setText] = useState("");
function callAPI(value: string) {
console.log(value);
// CALL API HERE
}
const debouncedAPI = _.debounce(callAPI, 2000);
function onChangeText(e) {
const value = e.target.value;
console.log(`value inside onChangeText: ${value}`);
setText(value);
}
return (
<div className="App">
<textarea
name="textarea"
id=""
value={text}
onChange={(e) => {
onChangeText(e); // Update the state
debouncedAPI(e.target.value); // Call the debounced API with the latest value
}}
></textarea>
</div>
);
}