Automatic batching or startTransition for safe rollback of a schema-driven UI state change in React 18
0 reputation · 08 Feb 2020, 15:57 UTC
0 reputation · 08 Feb 2020, 15:57 UTC
I have a React 18 app (concurrent renderer) where a single user action applies a "schema change" to local UI state: several components update their shape at once, driven by multiple setState calls, and a companion async fetch validates the new schema. If that fetch rejects, I want the UI to return to the previous consistent state rather than showing a half-migrated tree.
With automatic batching, all the updates commit in one render, which keeps the UI consistent, but I lose any intermediate loading state and I cannot tell whether the commit happened before or after the validation fetch settled. With startTransition, the update is interruptible and I can keep the old UI visible via isPending, but the scheduler decides when to yield, and if the async validation fails mid-transition there is no documented mechanism that guarantees the partially rendered work is discarded rather than committed.
Error boundaries only catch render-phase errors, and rejected promises inside a transition callback appear to bypass them, so the rollback path would have to be manual state restoration in a catch.
A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.