Forms are where users give your app data. In React, the usual pattern is a
controlled input: state holds the field's value, and React shows that value
in the field.
onChange runs on every keystroke. event.target is the input, and
event.target.value is the text it would now hold. The handler copies it into
state, React re-renders, and the field shows the new value.
Type in the field below. The character count and the preview follow every
keystroke, because they're computed from the same goal state during render.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Predict: value without onChange
Delete the onChange prop and run. Then try typing in the field.
Check your prediction
Nothing you type appears. value={goal} still says "show what's in state", and
nothing changes the state any more. The console explains: You provided a
value prop to a form field without an onChange handler. This will render a
read-only field. Put onChange back.
Why control the field?
Because the value lives in state, you can use it anywhere while the user types:
a live preview, a character count, a disabled button, or a corrected value.
Change the handler to keep the goal within the limit:
Now the field refuses to grow past 40 characters. The state decides what the
field shows.
Your turn
Exercise
Warn near the limit
Show the character count in the error class (red) once there are 35 or more
characters, and add (almost full) after it. Keep the slice from above so
the goal can't pass 40.