import { useState } from "react";
export default function App() {
const [height, setHeight] = useState('')
const handleKeyPressUp = event => {
const { value } = event.target
setHeight(value)
}
return (
<input
value={height}
onKeyPress={handleKeyPressUp}
onKeyUp={handleKeyPressUp}
/>
);
}
I've asked to use keypress
instead of onChange to listen the input change. And I use both onKeyPress
and onKeyUp
because of onKeyPress
is triggering right before the input value is changing(How to get text of an input text box during onKeyPress?), and also keypress will only be triggered for keys that produce a character value. i.e it won't be triggered on back space(https://developer.mozilla.org/en-US/docs/Web/API/Document/keypress_event).
But when assigning the state height
value to the input value, the input won't accept any input. Why the input value is not updated on typing text on the field?