Sub topic 4
Moving forward and back
This chapter covers forward and backward navigation in a guided flow. It assumes you can read TypeScript, know how forms and form fields work, and understand app state as a value that changes over time. It builds on the flow state model from the previous chapter: a current step id, collected data, a history list, and a list of errors.
Navigation as state transitions
Navigation is a pair of pure functions that take a FlowState and return a new one. They don't touch the screen. The UI calls them on button clicks and renders whatever state comes back. This keeps the rules in one place and makes them easy to test.
The two functions work differently:
- Forward is gated. It moves on only if the current step validates.
- Back is not gated. Users can leave a step with invalid or incomplete input, because going back doesn't submit anything.
The step definitions
Each step declares how to validate its data and which step comes next. The next function can read earlier answers, so branching lives in data instead of scattered if statements.
type Data = Record<string, string>;
type Step = {
id: string;
validate(data: Data): string[];
next(data: Data): string | null;
};
const steps: Record<string, Step> = {
account: {
id: "account",
validate: (d) =>
/^\S+@\S+\.\S+$/.test(d.email ?? "") ? [] : ["Enter a valid email address."],
next: (d) => (d.accountType === "business" ? "company" : "review"),
},
company: {
id: "company",
validate: (d) => ((d.company ?? "").trim() ? [] : ["Enter your company name."]),
next: () => "review",
},
review: {
id: "review",
validate: () => [],
next: () => null,
},
};A next that returns null marks the end of the flow. Here that is the review step.
Advancing and going back
The advance function validates first. If there are errors, it returns them and leaves the user on the same step. If validation passes, it pushes the current step id onto history and moves to the next id.
type FlowState = {
current: string;
history: string[];
data: Data;
errors: string[];
};
function advance(state: FlowState, steps: Record<string, Step>): FlowState {
const step = steps[state.current];
const errors = step.validate(state.data);
if (errors.length > 0) return { ...state, errors };
const nextId = step.next(state.data);
if (nextId === null) return { ...state, errors: [] };
return {
...state,
current: nextId,
history: [...state.history, state.current],
errors: [],
};
}
function back(state: FlowState): FlowState {
const previous = state.history[state.history.length - 1];
if (previous === undefined) return state;
return {
...state,
current: previous,
history: state.history.slice(0, -1),
errors: [],
};
}Both functions return new objects with spread syntax instead of changing the old one. That makes state changes easy to compare, undo, and test.
Why a history stack
You could compute the previous step by asking "which step comes before this one?" That breaks as soon as a flow branches. In the example, review can follow either account or company. Given only review, you can't tell which one the user came from.
The history stack records the path the user actually took. back pops the last entry, so the user returns to where they were, even after a branch.
The diagram below shows the sequence for a business user.
Common errors
- Recomputing the previous step. This gives the wrong answer after a branch. Use the history stack.
- Gating Back on validation. Users get trapped on a step they want to leave. Only forward navigation should validate.
- Leaving stale errors. Both functions reset
errorsto an empty list on a successful move. If you skip this, a message from one step shows up on the next. - Dropping data on Back. Keep
datauntouched when going back so the fields refill with what the user typed. - Stale history after changing an earlier answer. If a user goes back to
accountand switches frombusinesstopersonal, the data incompanyis still stored. Decide whether to clear it, or ignore it when you submit.
Edge cases to handle
- Pressing Back on the first step does nothing.
backreturns the same state whenhistoryis empty. - Pressing Next on the last step does nothing in
advance. The final step should offer a submit action instead, covered in a later chapter. - Double-clicking Next runs
advancetwice. The second call works on the new current step, so it may validate a step the user hasn't seen. Disable the button while rendering, or have the UI pass the step id it expects.
Exercise
A user on the account step picks "business", advances to company, then presses Back and changes the type to "personal". They press Next. What are current and history after that, and why is history correct even though company was visited earlier?
Show answer
current is review and history is ["account"]. Back popped account off the stack when returning, and the new Next pushed account once. The history records only the path now in effect, so company is not in it. The stored company value still sits in data and should be excluded or cleared before submission.
Exercise
Why does back skip validation, while advance requires it?
Show answer
Advancing commits the user's input and moves them deeper into the flow, so the current step must be valid first. Going back discards nothing and submits nothing, and requiring valid input would trap users on a step they want to leave.