Multi-Step Forms with Shared State
You'll learn: how to architect state for wizard-style forms that span multiple screens or steps.
The Architectural Challenge
When a form splits across multiple steps (like signup wizards, checkout flows, or surveys), you face a key design decision: where does all that form data live? Unlike single-page forms, multi-step forms need data from step 1 available when the user reaches step 3, and vice versa for validation or review screens.
Three Main Patterns
One Object for All Steps
Keep a single state object containing all fields from all steps:
const [formData, setFormData] = useState({
// Step 1 fields
email: '',
password: '',
// Step 2 fields
firstName: '',
lastName: '',
// Step 3 fields
address: '',
city: ''
});
Pros: Simple, all data accessible everywhere. Cons: Every component sees everything (even irrelevant fields).
Lifting State to Parent
The parent component holds the entire form state and passes down only what each step needs as props:
function MultiStepForm() {
const [currentStep, setCurrentStep] = useState(1);
const [formData, setFormData] = useState({...});
return (
<>
{currentStep === 1 && <Step1 data={formData} onChange={setFormData} />}
{currentStep === 2 && <Step2 data={formData} onChange={setFormData} />}
</>
);
}
Pros: Centralized control, easy navigation logic. Cons: Props drilling if steps are deeply nested.
Context Pattern
Wrap all steps in a context provider that exposes form state and update functions:
const FormContext = createContext();
function FormProvider({ children }) {
const [formData, setFormData] = useState({...});
return (
<FormContext.Provider value={{ formData, setFormData }}>
{children}
</FormContext.Provider>
);
}
Pros: No props drilling, clean step components. Cons: More boilerplate, harder to trace data flow.
Key Takeaway: Choose based on complexity—simple wizards benefit from lifted state in a parent, while complex multi-branch flows justify context to avoid props drilling through many layers.