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
- Separate lifecycle from validity. Each field tracks
touched(has it been blurred) anddirty(does its value differ from the initial one) independently from its validationstatus(pristine/validating/valid/invalid), so the UI can decide, say, not to show an error on a field the user hasn’t left yet. - Sequence async checks and discard stale ones. Every validation request for a field carries an incrementing sequence number; when a response comes back, it's only applied if its sequence still matches the field's latest — a response for an older value is silently dropped instead of overwriting newer state.
- Make submit read validity, not “absence of visible errors.” The submit gate requires every required field to be explicitly
valid(not merely not-yet-flagged-invalid) and checks a separatesubmittingflag, so a fast double-click can't fire two submissions and a not-yet-validated field can't sneak through. - Treat the async field's “in-flight” state as first-class.
validatinggets its own UI (a spinner, a disabled submit) rather than being lumped into either valid or invalid, which also made it obvious in testing when a slow network left the form stuck longer than expected. - Preserve form state on submit failure. A failed submit resets the
submittingflag and shows the error, but never clears field values — the cost of a transient failure shouldn’t be retyping a password. - Keep optional fields out of the gate. The bio field validates its own constraint (length) but never blocks submit on “untouched” or “empty,” which is a different rule from the required fields and worth encoding explicitly rather than special-casing in the submit handler.
Illustrative outcome
Figures below are illustrative, from working through this pattern on a representative signup flow — not a specific client’s production metrics.
- The stale-response bug (an old availability check overwriting a newer one) stopped reproducing once responses carried and checked a sequence number, including when QA deliberately throttled the network to make races likely.
- Separating lifecycle from validity meant the error-display logic needed far fewer special cases — most of the “don't show an error yet” bugs were really “touched is being used as a proxy for validity.”
- Because
submittingis explicit, the submit button couldn’t double-fire under rapid taps, which had shown up before as duplicate account-creation attempts.
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.