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

0.5.2 • Public • Published

Tweed Router

Universal Router for Tweed.

Installation

$ npm install --save tweed-router

Usage

const router = new Router({
  '/': () => new HomePage(),
  '/about': () => new AboutPage()
})
 
router.navigate('/')

Render the router with Tweed's render method to render the current page of the router automatically:

import { VirtualNode } from 'tweed'
import render from 'tweed/render/dom'
 
class HomePage {
  constructor (router) {
    this.router = router
  }
 
  render () {
    return (
      <p>
        Home page! Go to {this.router.link('the about page', '/about')}
      </p>
    )
  }
}
 
const router = new Router({
  '/': () => new HomePage(router),
  '/about': () => <p>About page</p>
})
 
router.navigate('/')
  .then(() => render(router, document.querySelector('#app')))

To hook up the router to the browser's History API, use the BrowserRouter like this:

import BrowserRouter from 'tweed-router/browser'
 
const routes = {
  '/': (router) => new HomePage(router),
  '/about': () => new AboutPage()
}
 
BrowserRouter.make(routes)
  .then((router) => render(router, document.querySelector('#app')))

The router can also be used on the server using the ServerRouter like so:

import ServerRouter from 'tweed-router/server'
 
ServerRouter.make(routes)
  .then((router) => router.handle(req, res))

Dependencies (1)

Dev Dependencies (7)

Package Sidebar

Install

npm i tweed-router

Weekly Downloads

4

Version

0.5.2

License

WTFPL

Last publish

Collaborators

  • emilniklas