storeon-isorouter

Isomorphic storeon module for url routing on client and server


License
MIT
Install
npm install storeon-isorouter@0.0.3

Documentation

Storeon Isomorphic Router

Build Status Coverage Status Code style: prettier npm npm bundle size unpkg

Storeon Router which solves the problem of routing your application on server and browser.

Installation

npm install storeon-isorouter
# or 
yarn add storeon-isorouter

Usage

Import the isorouter.createRouter from storeon-isorouter and add this module to createStore.

import createStore from 'storeon'
import isorouter from 'storeon-isorouter'

const store = createStore([
  isorouter.createRouter()
])

const Root = () => {
  const { [isorouter.key]: route } = useStoreon(isorouter.key)

  switch (route.path) {
    case "/":
      return <Main/>

    case "/blog/":
      return <Blog/>

    default:
      return <NotFound/>
  }
}

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

Define your own link component and subsribe route changing on click events.

import isorouter from 'storeon-isorouter'

const Link = ({href, children}) => {
  const { dispatch } = useStoreon(isorouter.key)

  const handleClick = (e) => {
    e.preventDefault()
    dispatch(isorouter.navigate, href)
  }

  return <a href={href} onClick={handleClick}>{children}</a>
}

API

import isorouter from 'storeon-isorouter'

const moduleRouter = isorouter.createRouter()

isorouter.key – key for store.

isorouter.navigate – navigation action.

LICENSE

MIT