In a mobile app with one-way data flow, how should a workout screen's state represent loading, stored content and a failed refresh at once?
answer
- state down, actions up
- one immutable state per screen
- loading and error are fields
- content and error can coexist
- the screen renders, never patches
basics
~20 sExpose one immutable screen state, rebuilt from the repository's stream, with content, a refreshing flag and an error as fields that can be true together; the screen renders it and sends user actions up, never editing its own copy.
solid answer
~40 sState flows down: the repository's stream of stored exercises and its refresh status are combined in the state holder into one immutable state, for example `exercises`, `isRefreshing`, `refreshError` and `lastSyncedAt`. Actions flow up: "log set", "retry" and "pull to refresh" are calls on the state holder, which passes them toward the owner of the data; the result returns as a new state. Modelling these as fields rather than one exclusive loading-or-content-or-error value matters offline: in a gym basement the screen should show today's plan *and* a "couldn't sync" notice, not swap the plan for an error page. Because the screen only ever renders the latest state, it can be redrawn or rebuilt at any moment from one value, and a late spinner can never hide fresh content.
code
pseudocode · 14 linesplan_screen_state = {
exercises: [ ... ], # from the store, may be hours old
is_refreshing: false,
refresh_error: "no connection", # shown as a notice, not a page
last_synced_at: "07:40"
}
function render(state):
if state.exercises is empty and state.refresh_error:
show_full_screen_message(state.refresh_error)
return
show_list(state.exercises)
if state.refresh_error:
show_notice(state.refresh_error)go deeper
Recall the two directions: state comes down to the screen, user actions go up. The screen draws what it is given and does not change data itself.
Explain why an offline-first screen models content, refreshing and error as fields that coexist, and walk one action up and the new state back down.
Show the bugs this prevents: call-order glitches, lost messages while the screen is hidden, two writers for one list. Say when an exclusive state type is still the better fit.
Standardise the screen-state shape and the handling of one-off messages across teams, so every screen behaves the same way offline and new screens cannot drift into command-style updates.
## One direction, two kinds of traffic **Unidirectional data flow** splits everything that moves between a screen and the rest of the app into two streams going opposite ways: - **State flows down.** The owner of the data (the repository, through the store) exposes it; the screen's state holder turns it into a screen state; the screen renders that state. - **Actions flow up.** A tap, a pull to refresh or a retry is sent upward as a call on the state holder, which forwards it to whoever owns the data that should change. - **Only the owner changes data.** Nothing below the owner edits a copy and hopes to sync it later. - **State is immutable.** Each change produces a new state value, so the screen never sees a half-updated object. The result is that a screen's appearance is a function of one value, not of the history of calls made to it. ## Modelling the screen's state The common first attempt is an exclusive type: *loading*, or *content*, or *error*. That shape is right when there is genuinely nothing to show until data arrives. An offline-first screen almost always has stored content, which changes the right shape. | Situation in the gym basement | Exclusive loading / content / error | Fields that can coexist | |---|---|---| | Opening the plan with stored data, refresh starting | Must pick: spinner hides the plan, or content hides the refresh | Plan shown, small refreshing indicator | | Refresh fails for lack of signal | Error replaces the plan the user needs | Plan shown, "couldn't sync, showing saved plan" notice | | First launch, nothing stored, offline | Error, correctly | Empty content plus error: render a full-screen message | | Refresh succeeds | Content | Plan updated, indicator cleared, error cleared | So a workout screen's state usually looks like `exercises`, `isRefreshing`, `refreshError`, `lastSyncedAt`, and the screen decides how to render each combination. The exclusive form remains a fine choice for a screen with no stored data, such as a one-time server lookup. ## Walking one action through the flow Logging a set shows both directions working together: 1. The user taps "log set"; the screen calls `onLogSet(weight, reps)` on its state holder and changes nothing itself. 2. The state holder calls the set repository, which records the set in the local store. Delivering it to the server, and marking it until the server accepts it, belongs to the offline-sync design. 3. The store reports the change; the repository's observable data for today's sets emits a new list. 4. The state holder combines the new list with the refresh status into a new immutable screen state. 5. The screen renders it, and the set appears with no special-case code for "just logged". Because the screen did not patch its own list, the set also appears on any other screen observing the same data, and there is no moment when the screen and the store disagree. ## One-off messages Some feedback is momentary, such as a "set saved" confirmation. One common approach models it as part of the state as well, a message field the screen shows and then reports as shown, so the message is not lost if it is produced while the screen is not visible. Another approach uses a separate one-time channel. Either way, keep the rule that the screen's visible content comes from state. ## Why it pays off - **Rebuildable screens.** Whenever the UI is redrawn or rebuilt, it renders the latest state and looks correct, with no calls to replay. - **No ordering bugs.** A "hide spinner" call arriving after a "show content" call cannot leave the screen wrong, because there are no such calls. - **Testable.** Feed a fake repository two values and assert the resulting states; no UI is needed. - **Debuggable.** Logging the state sequence explains what the user saw. ## Mistakes to avoid - Commands from the state holder to the screen, such as `showError()` and `hideSpinner()`, which make the display depend on call order. - A screen that appends the new set to its own list and then informs the state holder: two writers for one list. - Treating loading and error as "UI details" kept outside the state, so a rebuilt screen forgets them. - Hiding stored content whenever a refresh fails.
- Why not have the state holder call screen methods like showSpinner() and showError() directly?Then the display is the sum of every call made so far, and its correctness depends on call order. A call made while the screen is not visible is lost, a rebuilt screen has to replay history, and tests need a fake screen. With state, the latest value alone describes the screen.
- When is a single exclusive loading-or-content-or-error state the right model?When there is genuinely nothing to show until data arrives, such as a one-time server lookup with no stored result. For offline-first screens that usually have stored content, fields that can coexist fit better, because content and a refresh error are both true at once.
- The screen should hide stored content whenever a refresh fails.
- The screen can add the new set to its own list, then tell the view model.
- Loading and error are UI details, so they don't belong in the state.
- One-way data flow means the screen can never send anything back.
- Calling showError() on the screen is the same as exposing state.