# The flow and the stepper

> The three objects you work with, and how they relate.

Framework: Svelte (`@stepperize/svelte`), Stepperize v9.
Source: https://stepperize.com/docs/v9/svelte/concepts/stepper

Three words are used throughout these docs. They are different things.

| Name | What it is | Where it comes from |
| --- | --- | --- |
| **Definition** | The typed description of the steps, with functions and components bound to it. Holds no state. | `defineStepper(steps)` |
| **Flow** | One running instance: the current step, the data of every step, which steps are complete. | `useStepper()`, `Provider`, `Stepper.Root` or `create()` each create one |
| **Stepper** | What your component reads: a reactive object that shows the flow as it is now, plus the actions that change it. | Returned by `useStepper()` and `useStepperContext()` |

```svelte title="Checkout.svelte"
<script lang="ts">
  import { checkout } from "./checkout"; // the definition, made once in a module

  const stepper = checkout.useStepper(); // creates a flow, returns its stepper
</script>

<p>{stepper.current.id}</p>
```

## One flat object

Everything you read and everything you do is directly on the stepper. There
are no nested namespaces to learn.

```ts
stepper.current; // the step being shown, with your fields
stepper.index; // its position
stepper.data; // the data of every step
stepper.next(values); // save, validate and move forward
stepper.prev(); // go back
```

Most flows ship with seven members:

| Member | What it gives you |
| --- | --- |
| `current` | The step being shown. |
| `match({...})` | Exhaustive rendering by step id. |
| `next(data?)`, `prev()` | Move through the flow. |
| `isFirst`, `isLast` | What your Back and Continue buttons need. |
| `data` | What each step has saved. |

The rest (branching, snapshots, jumping to a step) is there when you need it.
The [full reference](https://stepperize.com/docs/v9/svelte/api/stepper) lists every member.

## Derived from one position

You only express intent: go next, go back, jump to review. The stepper derives
the consequences from the flow's state, so they can never disagree.

```ts
const flow = checkout.create(); // the same flow, without a component

flow.index; // 0
flow.isFirst; // true
flow.progress; // 0

await flow.next();

flow.index; // 1
flow.current.id; // "payment"
flow.state("shipping"); // "complete"
flow.progress; // 0.5
```

## The stepper shows the flow as it is now

Each change of the flow produces a new, immutable view of it. The stepper your
component holds is one object for its whole life, and every read goes to the
latest view.

Reads are tracked, so markup, `$derived` and `$effect` follow the flow.

Two consequences:

- Do not destructure its state. `const { current } = stepper` copies the step
  once and stops following the flow; read `stepper.current` where you need it.
- After an `await`, the stepper already shows the flow after the move.

```ts
async function onContinue() {
  const result = await stepper.next(values);

  stepper.current.id; // already the step the flow is on now
  if (result.accepted) result.to; // where the flow went, or null if it finished
}
```

Read what happened from the result: whether the move was accepted, why not,
and whether the flow finished. The stepper only tells you where the flow is.

## Who owns the flow

The component that creates the flow owns it and configures it.

| You write | The flow lives |
| --- | --- |
| `checkout.useStepper(options)` | In that component. Each call is a separate flow. |
| `<checkout.Provider linear onFinish={...}>` | In the provider, shared with everything below. |
| `<Stepper.Root linear onFinish={...}>` | In the root, shared with everything below. |
| `checkout.create(options)` | Wherever you keep the returned object. `fromFlow(flow)` reads it reactively. |

Options go on the owner. Components below a `Provider` or a `Stepper.Root`
read the flow with `useStepperContext()` and cannot reconfigure it.

See [Shared state](https://stepperize.com/docs/v9/svelte/guides/shared-state).

Next: [the lifecycle of a move](https://stepperize.com/docs/v9/svelte/concepts/moves).
