Email verification looks like a small feature until the UI starts moving under the user’s cursor.
The form begins with an email field, then shows a “check your inbox” message, then reveals a code input, then displays a resend timer. If each piece is rendered as an unrelated boolean, the page can jump between states and old messages can linger. Users get confused, and developers get a pile of edge cases.
This article shows a compact way to build the flow in React and TypeScript. The same approach works whether your test data comes from a real inbox, a fake email address, or a temporary inbox used only in development. The goal is a UI that feels steady even when the network is not.
Why email verification causes layout problems
A common first attempt looks something like this:
const [isSending, setIsSending] = useState(false); const [showCode, setShowCode] = useState(false); const [error, setError] = useState<string | null>(null); These flags can describe impossible combinations. For example, showCode may stay true while error is showing an error from a later resend request. A retry can also reveal the code field before the new message was accepted.
The layout problem is a symptom of a state problem. The browser is being asked to guess which message has priority.
It is easy to fix this by making the states explicit first, then deciding what each state can render. This gives the product a calmer feel and makes the implementation more simpler to reason about.
The state union also pay off when a new requirement arrives, because the missing transition is visible instead of hiding in another boolean.
Model the flow as explicit states
Use a discriminated union so TypeScript forces every state to be handled:
type VerificationState = | { kind: "idle"; email: string } | { kind: "sending"; email: string } | { kind: "sent"; email: string; expiresAt: number } | { kind: "verifying"; email: string } | { kind: "verified"; email: string } | { kind: "error"; email: string; message: string }; Now the UI has one source of truth. sent can show the code input, while sending can show progress and disable the submit action. A failed verification can return to sent with a useful message instead of accidentally returning to the email form.
Keep server actions separate from rendering. A request function should return a result or throw a known error; it should not reach into component state. That split makes the flow easier to reuse in a modal or a full-page onboarding screen later.
Keep the message region stable
Reserve a predictable area for status text. The message may be empty, but the region should still have a stable role and location:
<div className="status" aria-live="polite" aria-atomic="true"> {state.kind === "error" ? state.message : null} {state.kind === "sent" ? "We sent a verification code." : null} </div> Avoid placing the status inside a flex row that changes height when an error appears. A small min-height on the message region is often enough to stop the form from jumping. It’s a tiny CSS decision, but it saves alot of visual noise.
For screen reader users, aria-live="polite" lets the update be announced without interrupting the current task. Do not put every changing countdown value in the live region. A timer that announces every second is noisy and can make the form hard to use.
Keep this region near the form controls so it is still useable when the layout gets narrow.
The code input should also have a clear label, an inputMode="numeric", and an autocomplete hint such as one-time-code where supported. These details are small, but they remove friction on mobile.
A small React and TypeScript implementation
The submit handler can transition the state in a predictable sequence:
async function requestCode(email: string) { setState({ kind: "sending", email }); try { await api.sendVerificationCode({ email }); setState({ kind: "sent", email, expiresAt: Date.now() + 10 * 60 * 1000, }); } catch { setState({ kind: "error", email, message: "We could not send a code. Try again in a moment.", }); } } The UI can derive canSubmit from state.kind === "idle" || state.kind === "error". There is no second flag that might disagree with the state machine.
When verifying, preserve the email in the state. This avoids asking the user to type it again after an incorrect code. Also make resend a distinct event, rather than silently resetting the whole component. That keeps focus handling under your control.
The resend button need a clear disabled rule while its request is in flight, even if the visual design makes the button look quiet.
If the flow uses a temporary inbox or a fake email address in automated tests, give each test a unique address and clean it up afterward. For broader test-fixture concerns, see these notes on privacy budgets for test inboxes and debugging email tests that fail in CI.
Test the transitions, not just the happy path
Render the component with a small set of state-driven tests. Check that:
- submitting moves
idletosendingand disables the button; - a successful response shows the code input without moving the form’s main controls;
- an API error gives focus or a clear path back to the email field;
- an invalid code keeps the user in the verification step;
- resend does not create two active requests;
- a completed verification does not show a stale error message.
The exact test library matters less than the sequence. A test which only searches for the final success text can miss a broken intermediate state. I usually keep one test for keyboard navigation too, because mouse-only checks hide some layout and focus bugs.
These checks is especially useful when the same flow runs against a real service and a local test double.
Practical checklist
Before shipping a React email verification flow, confirm:
- The state model prevents contradictory flags.
- Status text has a stable place and an appropriate live-region behavior.
- Code inputs have labels, mobile-friendly hints, and useful autocomplete behavior.
- Loading, resend, and failure states cannot start duplicate requests.
- Tests cover transitions and recovery, not only success.
A verification form does not need a large state-management framework. A focused TypeScript union, a stable message region, and a few deliberate tests are usually enough. The result is a smoother feature for users and a much less confusing one for the next developer who opens the file.