Skip to content

[v5] Routable states#4184

Merged
davidkpiano merged 47 commits intomainfrom
v5/routes
Feb 12, 2026
Merged

[v5] Routable states#4184
davidkpiano merged 47 commits intomainfrom
v5/routes

Conversation

@davidkpiano
Copy link
Member

@davidkpiano davidkpiano commented Aug 9, 2023

Added routable states. States with route: {} and an explicit id can be navigated to from anywhere via a single { type: 'xstate.route', to: id } event.

const machine = setup({}).createMachine({
  id: 'app',
  initial: 'home',
  states: {
    home: { id: 'home', route: {} },
    dashboard: {
      initial: 'overview',
      states: {
        overview: { id: 'overview', route: {} },
        settings: { id: 'settings', route: {} }
      }
    }
  }
});

const actor = createActor(machine).start();

// Route directly to deeply nested state from anywhere
actor.send({ type: 'xstate.route', to: '#settings' });

Routes support guards for conditional navigation:

settings: {
  id: 'settings',
  route: {
    guard: ({ context }) => context.role === 'admin'
  }
}

@davidkpiano davidkpiano requested a review from Andarist August 9, 2023 13:45
@changeset-bot
Copy link

changeset-bot bot commented Aug 9, 2023

🦋 Changeset detected

Latest commit: 2788fb2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
xstate Minor
@xstate/react Major
@xstate/solid Major
@xstate/svelte Major
@xstate/vue Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 9, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@davidkpiano davidkpiano marked this pull request as draft August 9, 2023 19:00
@davidkpiano davidkpiano removed the request for review from Andarist August 9, 2023 19:00
@Andarist Andarist changed the base branch from next to main December 1, 2023 11:51
@christophe-g
Copy link

@davidkpiano

I am quite interested in this for binding the route with machine state actors.

Now that emit has landed (thanks!), would it make sense to emit a route event any time a state enters via actor.send({ type: 'xstate.route.anyRouteState')} ?

This would facilitate keeping the route in sync with the active state. Happy to provide more context if needed.

@davidkpiano
Copy link
Member Author

@christophe-g Glad you're interested in it!

Emitting events should be explicit, though, and it may be better to .subscribe(…) to the state changes and map those to URL states.

More context would be appreciated.

@christophe-g
Copy link

christophe-g commented Mar 20, 2024

More context would be appreciated.

@davidkpiano - this is not really more context, but a new lit controller binding xstate actor machine with a router.

and it may be better to .subscribe(…) to the state changes and map those to URL states.

I took your advice for this controller ...

Any change to have this PR merged ?

@christophe-g
Copy link

Dear @davidkpiano - just interested to know if this PR has any chance to be merged, or if there are any blocking issues ?

I have build a lit-controller around this (see above). This PR allows to de-couple business logic (handled by xstate machine), routing / navigation (natively handled by the browser), and views (provided by the router / also bound to state machine), which is great!

@davidkpiano
Copy link
Member Author

Dear @davidkpiano - just interested to know if this PR has any chance to be merged, or if there are any blocking issues ?

I have build a lit-controller around this (see above). This PR allows to de-couple business logic (handled by xstate machine), routing / navigation (natively handled by the browser), and views (provided by the router / also bound to state machine), which is great!

It does have a chance! Going to update it soon.

@christophe-g
Copy link

It does have a chance! Going to update it soon.

@davidkpiano - Fantastic, thank you !

@Hebilicious
Copy link

Hebilicious commented Nov 21, 2024

For anyone that needs this, I've implemented a verbose workaround:

  1. Create a routeTo property in the context for the route
  2. Define a guard per routable state that check for routeTo
  3. Create a RouteTo event with an array of all routes+ guard

Then you can route to a specific state by assigning routeTo and sending the RouteTo event

Routable state would simplify things massively though.
@davidkpiano Just an idea, but maybe the route should have a way to control where its connected to, something like this :

const todoMachine = createMachine({
  type: 'parallel',
  states: {
    todo: {
      initial: 'new',
      states: {
        new: {},
        editing: {}
      }
    },
    filter: {
      initial: 'all',
      states: {
        all: {
          route: { id: "filterRoute" }
        },
        active: {
          route: { id: "filterRoute" }
        },
        completed: {
          route: [{ id: "filterRoute" }, {id: "helloWorld"}]
        },
        hello: {
          route: { id: "helloWorld" }
        },
        world: {
          route: { id: "helloWorld" }
        }
      }
    }
  }
});

@christophe-g
Copy link

Routable state would simplify things massively though

Routable states are a hidden gem of xstate. Using with a lot of joy for instance in relatively complex stepper UI, allowing to bind the view with the state through the route.

@joernroeder
Copy link

I'd love to see this as part of xstates core. still plans to merge this in @davidkpiano?

@minhazfm
Copy link

This would be a huge help in keeping browser routing in sync with state machines! Thankful for your hard work @davidkpiano! Hope to see this available in V5 soon!

@statelyai statelyai deleted a comment Oct 9, 2025
@davidkpiano
Copy link
Member Author

I'd love to see this as part of xstates core. still plans to merge this in @davidkpiano?

Yes, I'd still like to see this merged in soon

@handletastic
Copy link

🙏🏼 What faith must I convert to for my plea to realise? 🥺 @davidkpiano

davidkpiano and others added 3 commits February 12, 2026 12:35
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
@davidkpiano davidkpiano merged commit a741fe7 into main Feb 12, 2026
1 check passed
@davidkpiano davidkpiano deleted the v5/routes branch February 12, 2026 19:43
@github-actions github-actions bot mentioned this pull request Feb 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants