Make React Email Flows Resilient at the UI Boundary
Email verification looks like a small feature until the UI has to explain what is happening. The user submits a form, waits for a message, opens a link, and expects the page to become useful again. In between, there can
Email verification looks like a small feature until the UI has to explain what is happening. The user submits a form, waits for a message, opens a link, and expects the page to become useful again. In between, there can be delivery delay, an expired token, a closed tab, or a temporary inbox that returns an older message.
The backend can be perfectly correct while the React screen still feels broken. A spinner that never changes, a generic βsomething went wrongβ message, or a disabled button with no reason creates confusion fast. In my experience, the fix is to treat the UI boundary as a real product contract instead of a thin reflection of a request promise.
This post shows a small TypeScript model for that contract. It keeps email polling, verification, and rendering separate, so each part can change without making the whole flow inconsistant.
The UI boundary is where email flows become confusing
An email flow usually has more states than loading, success, and error:
- The address was submitted, but no message is expected yet.
- The system is waiting for delivery.
- A message arrived, but its link is expired or belongs to another request.
- Verification succeeded, but the profile query is still loading.
- The user can request another message, but only after a cooldown.
When these cases are squeezed into one boolean, the UI starts guessing. A free temp email test might pass because the message arrived quickly, while a real user on a slow provider sees a dead-looking page. A throwaway email generator used in development can make this gap even harder to notice because the test inbox behaves unlike production.
I prefer an explicit state machine at the component boundary. It makes the happy path short and the odd paths visible during code review.
Model the verification states explicitly
Here is a deliberately small model. The state names describe what the user can do next, which is more useful than exposing transport details from the API.
type VerificationState =
| { kind: "idle" }
| { kind: "waiting"; address: string; sentAt: number; retryAfter: number }
| { kind: "verified"; address: string }
| { kind: "expired"; address: string }
| { kind: "failed"; address: string; message: string; canRetry: boolean };
type VerificationAction =
| { type: "submitted"; address: string; sentAt: number }
| { type: "verified"; address: string }
| { type: "expired" }
| { type: "failed"; message: string; canRetry: boolean }
| { type: "reset" };
The important bit is not the union syntax. It is that waiting carries the data needed to render a useful status, while failed says whether another action is valid. The button should not independently infer its disabled state from three unrelated hooks.
For a longer flow, a reducer keeps transitions in one place:
function reduceVerification(
state: VerificationState,
action: VerificationAction,
): VerificationState {
switch (action.type) {
case "submitted":
return {
kind: "waiting",
address: action.address,
sentAt: action.sentAt,
retryAfter: action.sentAt + 30_000,
};
case "verified":
return { kind: "verified", address: action.address };
case "expired":
return state.kind === "waiting"
? { kind: "expired", address: state.address }
: state;
case "failed":
return state.kind === "waiting"
? { ...state, kind: "failed", message: action.message, canRetry: action.canRetry }
: state;
case "reset":
return { kind: "idle" };
}
}
Now the component can render from one source of truth. It is also harder to show a success message for one email address while the request for a second address is still pending.
Keep polling and rendering separate
The component should not know how an inbox is polled. Give that responsibility to a small adapter that returns domain events. It could use an internal mail service, a test inbox, or a mocked client without changing the screen.
type VerificationEvent =
| { type: "verified"; address: string }
| { type: "expired" }
| { type: "failed"; message: string; canRetry: boolean };
async function waitForVerification(
client: VerificationClient,
address: string,
signal: AbortSignal,
): Promise<VerificationEvent> {
const result = await client.waitForResult({ address, signal });
if (result.status === "verified") {
return { type: "verified", address };
}
if (result.status === "expired") {
return { type: "expired" };
}
return {
type: "failed",
message: "We could not confirm that message yet.",
canRetry: true,
};
}
The adapter owns timeouts and cancellation. The React layer only dispatches the returned event. I also keep a clear stop rule for polling; a stop rule for TypeScript inbox polling is a small detail that prevents hidden work after the user leaves the page.
This split pays off in tests. You can test the reducer with immediate events, then test the adapter with fake time, without making every UI test wait for an inbox.
Make recovery useful instead of decorative
Recovery should tell the user what action is safe. For an expired link, show βRequest a new email.β For a delayed message, show the address and a refresh option. For a rate limit, show when the next request is allowed. Do not turn every state into βTry againβ because that shifts the diagnosis to the user.
If the flow uses external test data, also give it a privacy boundary. A privacy review for email test data is relevant even when the inbox contains only fake addresses; logs and screenshots can still leak identifiers.
Small wording details matter. A teammate may search for βtem emailβ or βtepm mail comβ while trying to find a temporary inbox. Those terms do not belong in a primary API contract, but supporting help text and search telemetry can reveal the words real developers use. Keep the actual UI clear and intentional.
Test the contract, not the timing
The best test cases assert visible behavior for each state:
- Submitting an address renders a waiting message and the masked address.
- A verified event renders the success path and removes the resend action.
- An expired event offers a new verification request.
- A retryable failure keeps the form usable and explains the next step.
- An abort does not dispatch an event after unmount.
Mock the adapter at the event boundary. You can still keep one end-to-end test with a real inbox, but most coverage should be fast and deterministic. This also avoids confusing a product regression with a delivery delay from a throwaway test account.
A short shipping checklist
Before shipping an email verification screen, I check:
- Is every user-visible state represented by a named type?
- Does the UI show what the user can do next?
- Can polling be cancelled when the component unmounts?
- Are retry and expiry rules enforced by one owner?
- Can reducer and adapter tests run without network timing?
- Are test addresses and message contents kept out of ordinary logs?
The goal is not a giant state machine. It is a boundary that makes uncertainty explicit. Once the UI knows whether it is waiting, expired, verified, or recoverable, the feature feels much more stableβeven when email delivery itself is not.
Q&A
Should the UI poll directly?
Usually no. A dedicated adapter makes cancellation, backoff, and provider changes easier to test. The component should render domain events, not manage inbox mechanics.
Do I need a reducer for a simple verification form?
Not always. A reducer becomes worthwhile when several events can arrive in different orders, or when more than one control depends on the same state. For a one-step form, a small hook can use the same explicit union without a reducer.
What is the first improvement to make in an existing flow?
List the states users can actually see, then replace the generic error boolean with those states one at a time. The clearer contract usually exposes the next reliability bug by itself.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.