How to Build a Multi-Step Form in React
When a form has many fields, splitting it into steps reduces the number of fields shown on screen at once. In React, if you keep the input values in a parent component and treat "Next" as a form submission, the values survive when the user goes back, and the browser's input validation and the Enter key work as they normally do. You also need to add type="button" to the "Back" button.
I built a three-step form ("Contact", "Shipping", "Confirm") in a test environment, operated it with Playwright on Chromium, and checked how the values persisted and how the buttons behaved.
The test environment is Ubuntu 24.04 with Node.js 22.22.0. I used react / react-dom 19.3.1, @types/react 19.3.0, typescript 7.0.2, and esbuild 0.28.2. I type-checked the .tsx files with tsc, transpiled them with esbuild, and operated Chromium 153 with Playwright 1.63.0. I used no libraries; the form was built only with React's useState and the browser's form features. I did not test Firefox or Safari, screen reader announcements, or actual submission to a server.

Keep the values in a parent component
Step components such as ContactStep exist on screen only while they are displayed, and they are removed when the step changes. As described in React's documentation on preserving and resetting state, when React removes a component, it also discards that component's state.
As a test, I wrote each step component with its own useState for the values and ran it in the test environment. I entered "Yamada" as the name, moved forward with "Next," and then went back with "Back." The name field was empty.
--- each step holds its values in its own state ---
「次へ」: 2/3 お届け先
「戻る」: 1/3 連絡先 / お名前: ""
So I put the values for all steps into a single type and kept them in a parent component that stays mounted when the step changes. The official documentation calls this form "lifting state up."
// types.ts
export type FormValues = {
name: string;
email: string;
address: string;
note: string;
};
export const initialValues: FormValues = { name: '', email: '', address: '', note: '' };
export const steps = ['連絡先', 'お届け先', '確認'] as const;
Each step component receives the values and a function that updates them as props, and nothing else. The update function is passed only the fields that changed.
// Steps.tsx (the contact step)
import type { FormValues } from './types';
type StepProps = {
values: FormValues;
onChange: (patch: Partial<FormValues>) => void;
};
export function ContactStep({ values, onChange }: StepProps) {
return (
<>
<label>
お名前
<input name="name" required value={values.name} onChange={(e) => onChange({ name: e.target.value })} />
</label>
<label>
メール
<input name="email" type="email" required value={values.email} onChange={(e) => onChange({ email: e.target.value })} />
</label>
</>
);
}
I wrote the shipping step (address and note) the same way, and the confirm step only displays values.
Treat "Next" as a form submission
The parent component looks like this. The key points are that there is one <form>, the "Next" button and the final "Send" button are both type="submit" buttons, and onSubmit advances the step.
// MultiStepForm.tsx
import { useEffect, useRef, useState, type FormEvent } from 'react';
import { ContactStep, AddressStep, ConfirmStep } from './Steps';
import { initialValues, steps, type FormValues } from './types';
export function MultiStepForm({ onSend }: { onSend: (values: FormValues) => void }) {
const [step, setStep] = useState(0);
const [values, setValues] = useState<FormValues>(initialValues);
const headingRef = useRef<HTMLHeadingElement>(null);
const shouldFocus = useRef(false);
// When the step changes, move focus to that step's heading
useEffect(() => {
if (!shouldFocus.current) return;
shouldFocus.current = false;
headingRef.current?.focus();
}, [step]);
const update = (patch: Partial<FormValues>) => setValues((v) => ({ ...v, ...patch }));
const goTo = (next: number) => {
shouldFocus.current = true;
setStep(next);
};
// Called only when the browser's input validation (required, etc.) passes
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (step < steps.length - 1) goTo(step + 1);
else onSend(values);
};
const isLast = step === steps.length - 1;
return (
<>
<ol className="progress">
{steps.map((label, i) => (
<li key={label} aria-current={i === step ? 'step' : undefined}>{label}</li>
))}
</ol>
<form onSubmit={handleSubmit}>
<h2 ref={headingRef} tabIndex={-1}>
{step + 1}/{steps.length} {steps[step]}
</h2>
{step === 0 && <ContactStep values={values} onChange={update} />}
{step === 1 && <AddressStep values={values} onChange={update} />}
{step === 2 && <ConfirmStep values={values} />}
<div className="actions">
{step > 0 && (
<button type="button" onClick={() => goTo(step - 1)}>戻る</button>
)}
<button type="submit">{isLast ? '送信する' : '次へ'}</button>
</div>
</form>
</>
);
}
The browser does not submit a form while an input fails its required or type="email" constraints. As described in MDN's submit event documentation, no submit event fires in that case, so handleSubmit is not called. Only the fields on the screen are checked, so there is no need to write per-step validation by hand.
In the test environment, pressing "Next" with the name field empty did not advance from the first step, and neither did pressing it with "yamada" typed into the email field. Pressing Enter in the email field advanced to the second step, the same as pressing "Next."
空のまま「次へ」: 1/3 連絡先 / お名前の validationMessage: Please fill out this field.
メールが「yamada」で「次へ」: 1/3 連絡先 / メールの typeMismatch: true
メールの欄で Enter: 2/3 お届け先 / フォーカス: h2 2/3 お届け先 / aria-current: お届け先
When I went back to the contact step with "Back," the name and email were still there. Pressing Enter in the address field moved to the confirm step, and pressing "Send" passed all the values to onSend as a single object.
住所を入れて「戻る」: 1/3 連絡先 / お名前: 山田 / メール: yamada@example.com
もう一度「次へ」: 2/3 お届け先 / 住所: 東京都
住所の欄で Enter: 3/3 確認 / お名前 山田 メール yamada@example.com 住所 東京都 備考 (なし)
送信: 送信しました:{"name":"山田","email":"yamada@example.com","address":"東京都","note":""}
The browser's input validation only helps the person filling in the form. As MDN's constraint validation documentation notes, values that fail these checks can still be sent, so validate the same rules on the server that receives the data.
Give the "Back" button type="button"
A <button> inside a <form> becomes a submit button when no type is set (MDN's button element documentation). If you forget to set type on "Back," clicking it moves back one step and also submits the form, provided the previous step also has a "Back" button.
In the test environment, I built a simpler form with one input per step and left type off the "Back" button. This form displays the number of times submit has fired. On the confirm step, pressing "Back" increased the submit count by one, and the screen stayed on the confirm step. The onClick moved back one step, and then the submit that followed advanced the form again through this form's handleSubmit. On the second step, pressing "Back" did not fire a submit. My guess is that this is because "Back" disappears from the screen when you return to the first step.
「戻る」の type プロパティ: submit
2回「次へ」: 3/3 確認 / submit 2回
3つ目で「戻る」: 3/3 確認 / submit 3回
2つ目で「戻る」(戻ると「戻る」ボタンが消える): 1/3 連絡先 / submit 1回
In the MultiStepForm above, I advanced to the confirm screen and then removed the type attribute from the "Back" button and pressed it. The screen stayed on the confirm step and did not move.
Enter key behavior causes another problem. The HTML specification says that pressing Enter in an input field activates the first submit button in the form. If "Back" comes before "Next" and has no type, the first submit button is "Back." In the test environment, pressing Enter in the input on the second step returned to the first step.
2つ目の入力欄で Enter: 1/3 連絡先 / submit 1回
A "Back" button with type="button" is not a submit button, so Enter activates only "Next." As the results in the previous section show, pressing Enter in the address field advanced to the confirm step.
Move focus to the heading when the step changes
When you switch steps, the element that had focus can be removed from the screen, as happens when you press Enter in an input field. In MultiStepForm, after the step changes, focus moves to that step's heading so that keyboard users do not lose track of where the new step begins. A heading is not normally focusable, so it gets tabIndex={-1}. As MDN's tabindex documentation describes, -1 keeps the element out of the Tab order while still allowing it to be focused from a script.
To avoid moving focus when the page first opens, I set shouldFocus only in goTo and call focus() in the effect only when it is set. As the "Enter in email field" line in the results above shows, after advancing to the second step, focus was on the heading (h2).
In the list of steps, I put aria-current="step" only on the item for the current step. MDN's aria-current documentation lists step as the value for telling assistive technology which step is current in a multi-step process, when that step is otherwise indicated only visually. In the test environment, the item with aria-current moved from "Contact" to "Shipping" with each step forward.





