Skip to content

Instantly share code, notes, and snippets.

@sawyerh
Last active June 27, 2021 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sawyerh/c0fe0aeeec7f2b91ccf68c2a29f07f65 to your computer and use it in GitHub Desktop.
Save sawyerh/c0fe0aeeec7f2b91ccf68c2a29f07f65 to your computer and use it in GitHub Desktop.
Router.js: Routing using state machines
// router.js
import { routingMachine } from "machine";
export function getNextPathname(event, context) {
const currentPathname = window.location.pathname;
const nextPathname = routingMachine
.withContext(context) // https://xstate.js.org/docs/guides/context.html
.transition(currentPathname, event); // https://xstate.js.org/api/interfaces/statemachine.html#transition
return nextPathname;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment