0
0
Reactframework~10 mins

Form handling in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Form handling in React
User types in input
onChange event triggers
Update state with new input
Component re-renders
Input shows updated value
User submits form
onSubmit event triggers
Handle form data (e.g., alert or send)
Prevent default page reload
The flow shows how user input updates React state, causing re-render to reflect changes, and how form submission is handled without page reload.
Execution Sample
React
function MyForm() {
  const [name, setName] = React.useState('');
  return (
    <form onSubmit={e => { e.preventDefault(); alert(name); }}>
      <input value={name} onChange={e => setName(e.target.value)} />
      <button type="submit">Submit</button>
    </form>
  );
}
A simple React form that updates state as user types and alerts the name on submit without reloading.
Execution Table
StepActionState BeforeEventState AfterRendered Input ValueOutput
1Initial rendername = ''nonename = '''' (empty input)none
2User types 'J'name = ''onChange with value 'J'name = 'J''J'none
3User types 'Jo'name = 'J'onChange with value 'Jo'name = 'Jo''Jo'none
4User types 'Jon'name = 'Jo'onChange with value 'Jon'name = 'Jon''Jon'none
5User clicks Submitname = 'Jon'onSubmit eventname = 'Jon''Jon'alert('Jon') shown
6Form submission preventedname = 'Jon'onSubmit event with e.preventDefault()name = 'Jon''Jon'no page reload
💡 Form submission stops default reload; alert shows current input value.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
name'''J''Jo''Jon''Jon''Jon'
Key Moments - 3 Insights
Why does the input value update when I type?
Because onChange updates the state variable 'name' (see steps 2-4 in execution_table), and React re-renders the input with the new value.
Why doesn't the page reload when I submit the form?
Because e.preventDefault() is called in the onSubmit handler (step 6), stopping the browser's default reload behavior.
What happens if I don't set the input's value to the state variable?
The input becomes uncontrolled and won't reflect React state changes, breaking the connection between input and state (not shown in table but important).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'name' after step 3?
A'Jo'
B'Jon'
C'J'
D''
💡 Hint
Check the 'State After' column in row with Step 3.
At which step does the form submission alert show the input value?
AStep 4
BStep 6
CStep 5
DStep 2
💡 Hint
Look at the 'Output' column for alert in the execution_table.
If e.preventDefault() was removed, what would happen at step 6?
AThe alert would not show
BThe page would reload after submit
CThe input value would reset to empty
DThe state would change to empty string
💡 Hint
Refer to the 'exit_note' and step 6 explanation about preventing page reload.
Concept Snapshot
React form handling:
- Use useState to store input value
- onChange updates state with e.target.value
- Input's value prop set to state variable
- onSubmit handler prevents default reload
- Handle form data inside onSubmit
- Keeps UI and state in sync
Full Transcript
This visual trace shows how React handles form input and submission. When the user types, the onChange event updates the state variable 'name'. React re-renders the input with the updated value, keeping the UI and state synchronized. On form submission, the onSubmit event triggers, calling e.preventDefault() to stop the page from reloading. The current input value is then alerted. This flow ensures smooth user experience without page refreshes.