eli8levit / router

Storeon module for URL routing

Home Page:https://storeon.github.io/router/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Storeon Router

Storeon logo by Anton Lovchikov

Storeon Router which solves the problem of routing your application, providing full control over the route.

It size is 635 bytes (minified and gzipped) and uses Size Limit to control size.

Installation

npm install @storeon/router
# or 
yarn add @storeon/router

Usage

If you want to use the router you should import the router.createRouter from @storeon/router and add this module to createStore.

import createStore from 'storeon'
import router from '@storeon/router'

const store = createStore([
  router.createRouter([
    ['/', () => ({ page: 'home' })],
    ['/blog', () => ({ page: 'blog' })],
    ['/blog/post/*', (id) => ({ page: 'post', id })],
    [
      /^blog\/post\/(\d+)\/(\d+)$/,
      (year, month) => ({ page: 'post', year, month })
    ]
  ])
])

function Root() {
  const { [router.key]: route } = useStoreon(router.key)

  switch (route.match.page) {
    case "home":
      return <Home/>

    case "blog":
      return <Blog/>

    case "post":
      return <Post year={route.match.year} month={route.match.month} id={route.match.id}/>

    default:
      return <NotFount/>
  }
}

store.dispatch(router.navigate, '/')

API

import router from '@storeon/router'

const moduleRouter = router.createRouter([
  [path, callback]
])

Function router.createRouter could have options:

  • path: path name can be a string or RegExp.
  • callback: the callback function must return an object with parameters for this path.

router.key – key for store.

router.navigate – navigation action.

router.changed – change event of pathname.

Sponsor

Sponsored by Evrone

LICENSE

MIT

About

Storeon module for URL routing

https://storeon.github.io/router/

License:MIT License


Languages

Language:JavaScript 94.9%Language:HTML 5.1%