← BYTES
React.js·Intermediate·Ref: 4.2.9
For a controlled text input, which pattern correctly keeps the input's displayed value in sync with React state?
Have a question about this video? Mention reference 4.2.9 when you contact us.
a.Set `defaultValue` once and never update it again
b.Set `value={stateValue}` and update `stateValue` inside an `onChange` handler reading `event.target.value`
c.Read the DOM value only when the form is submitted, with no `value` or `onChange` at all
d.Controlled inputs cannot have their value changed by the user at all
A controlled input binds its displayed `value` to a state variable, and the `onChange` handler reads the new value out of the native event (`event.target.value`) and feeds it back into that same state variable -- keeping React as the single source of truth for what's on screen.
Find this on YouTube & Instagram
Every BYTES video is tagged with its reference number and topic — search any of these on YouTube or Instagram to find this one:
#BYTES#BYTES429#Reactjs