Skip to content

feat: start over from any step with reset overrides - #12

Merged
bcheung merged 2 commits into
mainfrom
feat/reset-options
Nov 6, 2026
Merged

bcheung merged 2 commits into
mainfrom
feat/reset-options

Conversation

@bcheung

@bcheung bcheung commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Description

reset() always returns to the context and start step the flow mounted with. There's no way to start over with different data or at a different step: for example a review screen with a "Start fresh" button that should clear the answers and go back to the first question, without leaving the review screen in history.

The workaround is to build state by hand with createInitialState from @useflow/core and pass it to restore(). That pulls in the core package, and it skips the cleanup reset() does (clearing saved progress).

reset now takes optional overrides:

// Flow mounted at "review" with prefilled answers
<Flow flow={quizFlow} initialStepId="review" initialContext={prefilled}>
  {({ reset, context }) => (
    <button onClick={() => reset({ context: { ...context, answers: [] }, initialStepId: "questions" })}>
      Start fresh
    </button>
  )}
</Flow>
  • context replaces the initial context; initialStepId replaces the start step (typed to the flow's step ids).
  • Saved progress is cleared and history starts fresh, as with a plain reset().
  • reset() with no arguments is unchanged, and later plain resets still use the mount values.
  • The reducer's RESET action already accepted both values.

Restoring progress that started at another step

Restores used to reject saved progress whose path didn't begin at start (or the mounted initialStepId). After a reset to another step, the next launch would discard the user's progress as incompatible. That check is removed: a path can begin at any existing step. Missing steps, a current step that doesn't match the path, and an inconsistent status are still rejected; structural changes remain the job of version + migrate.

This removes the allowedInitialStepIds option of validatePersistedState (and the ValidatePersistedStateOptions type), which only existed to widen that check.

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 💥 Breaking change
  • 📝 Documentation
  • ♻️ Refactoring
  • ✅ Tests

Checklist

  • Tests pass (bun run test)
  • Types are valid (bun run typecheck)
  • Code is formatted (bun run check)
  • Added/updated tests
  • Updated docs (if needed)
  • Added changeset (if needed - run bun changeset)

Testing

  • bun run test: 391 tests pass. New tests: a saved flow reset to another step resumes there after remounting (fails with the old check), and a test that resets with overrides from the last step and checks the new step, context, empty history (canGoBack false) and saved-state removal, then checks a plain reset() still returns to the mount values.
  • validatePersistedState accepts a path starting at a non-start step.
  • Type tests: reset({ initialStepId }) only accepts known step ids.
  • Docs build passes.

@bcheung bcheung changed the title feat(react): accept context and start step overrides in reset feat: start over from any step with reset overrides Nov 6, 2026
@bcheung
bcheung force-pushed the feat/reset-options branch from 7af84b1 to e43ace1 Compare November 6, 2026 03:57
@bcheung
bcheung marked this pull request as ready for review November 6, 2026 03:57
@bcheung
bcheung merged commit 827436d into main Nov 6, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant