A multi-step form equals one step state, conditional rendering of the current step and a single form state that holds the data of every step. Each step validates locally before you may continue, the last step submits everything at once, and switching steps never loses what the user already typed. Keeping every field in one shared object is what makes the back button free: the step changes, but the data simply stays where it is and the user is free to correct an earlier answer.
Group
Component Patterns
Kind
Pattern
ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.
📝 Syntax
const [step, setStep] = useState(0) // index of the current step
const [data, setData] = useState({}) // form data shared across steps
// conditionally render step number "step"; Back and Next only change step
Returns:No return value; this entry describes an encoding pattern rather than a function.
▶ Example
The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.
Output:
"Next" only becomes available after a name is filled in on step 1; once a city is entered on step 2 the form can be submitted; the confirmation screen summarizes the data of both steps, and the step indicator stays visible at the bottom.
💡 Tip Keep the data of all steps in one object state so switching steps never loses it; using disabled for the per-step check is the simplest way to make the user pass before moving on.
ℹ️ Info With many steps, extract the configuration into an array and render it with map, or lift the step into a route parameter so users can go back and forth and share links.
⚠️ Warning Reloading the page loses both the step and the data, so persist them to sessionStorage or wrap them in a custom hook when users must be able to resume later.
A No. As long as every step shares the same form state, step only controls which step is displayed and the data stays where it is.
Q How do I build a step progress indicator?
A Map over an array of step configurations and compare the index with the current step to decide the finished, active or not-started styling.
Q How do I validate everything before submitting?
A On the last step run one validate function over the data of all steps; if it reports errors, setStep jumps back to the matching step to highlight them.
Web-Tutorial Tech Team
A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.