Suppose I have a react component with a controlled form input, ex:
const MyInputs = () => {
const [val, setVal] = useState("")
const handleChange = (event: ChangeEvent<HTMLInputElement>): void => {
setVal(event.target.value) // what is event.target.value here?
}
return (
<form>
<input value={val} />
</form>
)
}
My understanding is that, since I provide no onChange
handler to the input
, the input box in the browser will not show any keystrokes. I can type all I want but nothing will ever show up in the input box.
Contrast this with a regular, vanilla HTML <input value="something" />
. Typing characters in the input box will result in characters appearing on screen and changes to the value
attribute of the input.
Is React 'intercepting' change events on controlled components to actively prevent the input's
value
from changing on keyboard input?if the
value
of the controlled input can only change in response to changes to component state, how is it thatevent.target.value
can provide us with "the current text that the user has entered"?If React does intercept events per Q1, are these handlers overridden by event handlers that I manually attach to a controlled input's
onChange
attribute?what is the difference (in terms of browser behavior) between "displaying characters that a user typed in a vanilla html input field" and "re-rendering as a result of component state changes (caused by user keyboard input)"?
EDIT
I understand that value
in <input value=... />
is not the same as $myInputElement.value
. But The value attribute is a string that contains the current value of the text entered into the text field. You can retrieve this using the HTMLInputElement value property in JavaScript..