Sub topic 1
When a Guided Flow Is the Right Tool
Prerequisites
This chapter assumes basic programming, familiarity with form fields, and a basic understanding of application state. Examples use framework-neutral TypeScript.
When to Use a Guided Flow
A guided flow collects information across several screens in a deliberate order. It is useful when users must complete related decisions, when later questions depend on earlier answers, or when one long form would feel difficult to scan.
A single form is usually better for short, independent input. Use a guided flow when grouping fields helps users understand why each question matters. Each step should have one clear purpose, such as “Account details” or “Delivery address.”
Splitting Input into Steps
Split fields by the user's task, not by your database tables. A registration flow might use these steps:
- Account: email and account type
- Company: company name, only for business accounts
- Review: confirm all entered values
The flow should move from general information to decisions and details. Avoid making a step so small that it creates unnecessary navigation.
Modeling Flow State
Flow state is the data needed to render and control the flow. Keep the current step, collected data, navigation history, and visible errors together.
type Data = Record<string, string>;
type FlowState = {
current: string;
history: string[];
data: Data;
errors: string[];
};
const initialState: FlowState = {
current: "account",
history: [],
data: {},
errors: [],
};
console.log(initialState.current); // accountThe current value identifies what to display. The data object preserves answers between screens. The history array records the route the user actually took, which matters when a flow branches.
Moving Forward and Back
Forward navigation should validate the current step before changing current. When it succeeds, append the current step to history. Back navigation removes the last history item and returns to that step.
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: [],
};
}This history-based approach is safer than guessing the previous screen from a fixed step number. It preserves the user's real path through conditional steps.
Exercise
Why should Back use navigation history instead of simply decrementing a step number?
Show answer
A branch can skip or add steps, so history returns the user to the screen they actually visited.