0.5.2 • Published 7 years ago

tweed-router v0.5.2

Weekly downloads
3
License
WTFPL
Repository
github
Last release
7 years ago

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))
0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.11

7 years ago

0.3.10

7 years ago

0.3.9

7 years ago

0.3.8

7 years ago

0.3.7

7 years ago

0.3.6

7 years ago

0.3.5

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago