use-router-machine
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

use-router-machine

Hook that let's you use XState as a router, by using the meta property.

Live example: https://codesandbox.io/s/8n06pwy61l

Use

Install the library with npm i use-router-machine.

import { useRouterMachine } from 'use-router-machine'
 
const config = {
  initial: 'home',
  states: {
    home: { meta: { path: '/' }, on: { NEXT: 'about' } },
    about: { meta: { path: '/about' }, on: { NEXT: 'dashboard' } },
    dashboard: {
      meta: { path: '/dashboard' },
      initial: 'login',
      on: { NEXT: 'home' },
      states: {
        loggedIn: {
          initial: 'main',
          states: {
            main: { meta: { path: '/dashboard/main' } },
            data: { meta: { path: '/dashboard/data' } }
          }
        },
        login: {
          meta: { path: '/dashboard/login' },
          on: { LoggedIn: 'loggedIn' }
        }
      }
    }
  }
}
 
function App() {
    const service = useRouterMachine({ config })
 
    return <div>{service.state.value}</div>
}

Package Sidebar

Install

npm i use-router-machine

Weekly Downloads

7

Version

0.2.1

License

MIT

Unpacked Size

4.98 kB

Total Files

5

Last publish

Collaborators

  • carloslfu