Lesson 5 of 9
Multi-Step Forms
Registration flows, onboarding, and checkout typically span multiple screens. Multi-step forms manage partial validation per step and accumulate data across steps in a shared store or parent state.
Multi-Step Form Pattern
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
import { View, Text, Pressable, StyleSheet } from 'react-native';
// ---- Schemas per step ----
const step1Schema = z.object({
firstName: z.string().min(1, 'Required'),
lastName: z.string().min(1, 'Required'),
});
const step2Schema = z.object({
email: z.string().email('Invalid email'),
phone: z.string().regex(/^+?[ds-]{10,}$/, 'Invalid phone number'),
});
const step3Schema = z.object({
password: z.string().min(8, 'Min 8 characters'),
confirmPassword: z.string(),
}).refine(d => d.password === d.confirmPassword, {
message: 'Passwords must match',
path: ['confirmPassword'],
});
type Step1 = z.infer<typeof step1Schema>;
type Step2 = z.infer<typeof step2Schema>;
type Step3 = z.infer<typeof step3Schema>;
type FormData = Step1 & Step2 & Step3;
const STEPS = ['Personal', 'Contact', 'Security'];
export function MultiStepSignup() {
const [step, setStep] = useState(0);
const [formData, setFormData] = useState<Partial<FormData>>({});
const schemas = [step1Schema, step2Schema, step3Schema];
const { control, handleSubmit, formState: { errors } } = useForm({
resolver: zodResolver(schemas[step]),
defaultValues: formData,
mode: 'onBlur',
});
function onNext(data: Partial<FormData>) {
setFormData(prev => ({ ...prev, ...data }));
if (step < STEPS.length - 1) setStep(s => s + 1);
else submitAll({ ...formData, ...data } as FormData);
}
function submitAll(data: FormData) {
console.log('Final submission:', data);
}
return (
<View style={styles.container}>
{/* Progress indicator */}
<View style={styles.progressRow}>
{STEPS.map((label, i) => (
<View key={label} style={styles.stepIndicator}>
<View style={[styles.stepDot, i <= step && styles.stepDotActive]}>
<Text style={[styles.stepNum, i <= step && styles.stepNumActive]}>
{i < step ? '✓' : String(i + 1)}
</Text>
</View>
<Text style={[styles.stepLabel, i === step && styles.stepLabelActive]}>{label}</Text>
</View>
))}
</View>
{/* Step content - rendered conditionally */}
<View style={styles.content}>
{step === 0 && <Step1Fields control={control} errors={errors} />}
{step === 1 && <Step2Fields control={control} errors={errors} />}
{step === 2 && <Step3Fields control={control} errors={errors} />}
</View>
{/* Navigation */}
<View style={styles.nav}>
{step > 0 && (
<Pressable style={styles.backBtn} onPress={() => setStep(s => s - 1)}>
<Text style={styles.backBtnText}>← Back</Text>
</Pressable>
)}
<Pressable style={styles.nextBtn} onPress={handleSubmit(onNext)}>
<Text style={styles.nextBtnText}>{step === STEPS.length - 1 ? 'Submit' : 'Next →'}</Text>
</Pressable>
</View>
</View>
);
}
// Placeholder step components
function Step1Fields({ control, errors }: { control: unknown; errors: unknown }) {
return <Text style={{ color: '#f1f5f9' }}>Step 1: Personal info fields here</Text>;
}
function Step2Fields({ control, errors }: { control: unknown; errors: unknown }) {
return <Text style={{ color: '#f1f5f9' }}>Step 2: Contact info fields here</Text>;
}
function Step3Fields({ control, errors }: { control: unknown; errors: unknown }) {
return <Text style={{ color: '#f1f5f9' }}>Step 3: Security fields here</Text>;
}
const styles = StyleSheet.create({
container: { flex: 1, backgroundColor: '#0f172a', padding: 24 },
progressRow: { flexDirection: 'row', justifyContent: 'center', gap: 24, marginBottom: 32 },
stepIndicator: { alignItems: 'center', gap: 6 },
stepDot: { width: 32, height: 32, borderRadius: 16, borderWidth: 2, borderColor: '#334155', alignItems: 'center', justifyContent: 'center' },
stepDotActive: { backgroundColor: '#229c6b', borderColor: '#229c6b' },
stepNum: { fontSize: 13, fontWeight: '700', color: '#475569' },
stepNumActive: { color: 'white' },
stepLabel: { fontSize: 11, color: '#475569' },
stepLabelActive: { color: '#229c6b', fontWeight: '600' },
content: { flex: 1 },
nav: { flexDirection: 'row', gap: 12, paddingBottom: 16 },
backBtn: { flex: 1, borderWidth: 1.5, borderColor: '#229c6b', borderRadius: 12, padding: 14, alignItems: 'center' },
backBtnText: { color: '#229c6b', fontWeight: '700' },
nextBtn: { flex: 2, backgroundColor: '#229c6b', borderRadius: 12, padding: 14, alignItems: 'center' },
nextBtnText: { color: 'white', fontWeight: '700', fontSize: 16 },
});Tip
Tip
Practice MultiStep Forms in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Multi-step registration workflow with progress indicators and persistent state
Practice Task
Note
Practice Task - (1) Write a working example of MultiStep Forms from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with MultiStep Forms is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?