
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
xstate-router
Advanced tools
XState Router. Add routes to your XState machine and maintain it in sync with the actual route.
If you want to use this solution with hooks use-router-machine.
Install the library with npm i xstate-router.
If you don't have XState installed, install it: npm i xstate
Try the live example here: https://codesandbox.io/s/rllly3pyxp.
The routerMachine function returns an interpreter:
import { routerMachine } from 'xstate-router'
const machineConfig = {
initial: 'main',
context: { myValue: 0 },
states: {
main: { meta: { path: '/' } },
blog: { meta: { path: '/blog' } },
},
}
const service = routerMachine({
config: machineConfig,
options,
initialContext,
})
// The state changes on a route change and the route changes on a state change.
service.onTransition(state => console.log(state.value))
// The context is enhanced with router properties.
service.onChange(ctx => console.log(ctx))
/* Context
{
myValue: 0,
// Router properties:
match,
location,
history,
}
*/
null (not matching), {} (no parameters), { param1: 4711 }history.locationrouterMachine(...) accepts a history object as fourth parameter. If it is missing it defaults to createBrowserHistory() (from package 'history') and is published in the context.if you translate to a state having a parameterized route then you have to ensure that context.match contains the values of those parameters. Otherwise the placeholder is shown in the route. Example:
states: {
list: { meta: { path: '/items' },
on: {
ShowDetails: {
target: 'details',
actions: assign((ctx, event) => ({
...ctx,
match: { id: event.item }
}))
}
}
}
details: { meta: { path: '/items/:id:/details'} }
}
where the event trigger could look like this:
<button onClick={() => this.send('ShowDetails', { item: 817 })}>Show details...</button>
Paths could have parameters such as /items/:id:/details and regular expressions, for more information please read this: https://github.com/pillarjs/path-to-regexp.
If a route changes then a parameterized event 'route-changed' is fired: e.g. { dueToStateTransition: "true", route: "/blog", service: /* the xstate interpreter */ }.
dueToStateTransition is true. If the route changes because the location was changed (either by the user in the browsers location bar or by a script changing history.location), then dueToStateTransition is false.route gives you the current route which causes the eventservice provides the xstate interpreter which can be used to send another event.Placing an on: 'router-changed' event at a state can be used to avoid leaving the current state if the route changes. Think of a state which might show unsaved data and you want to ask the user 'Leave and loose unsaved data?'. If you decide to accept the new route anyway you have to resend the event:
on: {
'route-changed': {
cond: (context, event) => event.dueToStateTransition === false
&& !event.processed, // interfere only new events
actions: (context, event) => {
if (context.unsavedData) return; // suppress current route change
event.processed = true; // mark event as processed
event.service.send(event); // resend the event to establish the origin route change
}
}
},
FAQs
XState Router. Add routes to your XState machine.
The npm package xstate-router receives a total of 495 weekly downloads. As such, xstate-router popularity was classified as not popular.
We found that xstate-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.