← Back to portfolio

React Native · React · State management

Form state that stays correct under slow networks and fast typing

A signup form with one async-validated field (username availability) kept producing stale error states — a field would show “available” after the user had already typed something else. The fix was treating field state as a small explicit machine instead of a bag of booleans.

Context

The form itself was simple: username, email, password, a short bio. Most of the validation was synchronous — format checks that run instantly against the current value. Username was the exception: availability had to be checked against the server, which meant every keystroke could, in principle, kick off a request, and those requests didn’t all come back in the order they were sent.

Problem

Early versions tracked each field with little more than value and error. That was enough until the async check landed: a user would type jun, pause long enough to fire the availability request, then keep typing to jun_dev before the first response returned. If that stale response resolved after the newer one, it could overwrite a correct “taken” with an incorrect “available” for a username the user had already changed. A second, quieter problem: the submit button’s enabled state was computed from “are there any visible errors,” which doesn’t distinguish a field that’s actually valid from one that simply hasn’t been validated yet — so a fast submitter could beat validation entirely.

Approach — a small state machine per field, not a flag

Illustrative outcome

Figures below are illustrative, from working through this pattern on a representative signup flow — not a specific client’s production metrics.

What I took from it

Most form bugs aren’t validation-rule bugs; they're state-shape bugs. A field has more than one axis of state — has it been interacted with, does it differ from its initial value, is it currently being checked, is it actually valid — and collapsing those into a single error string is what makes async validation feel fragile. The fix isn’t a bigger form library; it’s being honest about how many states a field can be in and sequencing the async ones so a slow response can never land after a faster, newer one.

The interactive demo below exposes that machine directly: watch a field move through pristine, touched, dirty, validating, valid and invalid, dial up the async check's latency, and try to beat the submit gate with partial input.

Try the form state demo →