Skip to content

xrado/cherrytree-for-react

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cherrytree-for-react

Use the cherrytree router in your React applications. This project provides a React component that you should put at the root of your render tree. The component handles hot reloading.

Usage

$ npm install --save cherrytree cherrytree-for-react
import React from 'react'

import createCherrytree from 'cherrytree'
import { Router, Link } from 'cherrytree-for-react'
import * as components from './components'

import { Provider } from 'redux/react'
import { createDispatcher, createRedux, composeStores } from 'redux'
import * as stores from './stores'

const {
  Application,
  About,
  GithubStargazers,
  GithubRepo,
  GithubUser
} = components

const redux = createRedux(createDispatcher(composeStores(stores)))

const cherrytree = createCherrytree()
  .map(routes)
  .use(function redirect (transition) {
    let lastRoute = transition.routes[transition.routes.length - 1]
    if (lastRoute.options.redirect) {
      cherrytree.replaceWith.apply(cherrytree, lastRoute.options.redirect)
    }
  })
  .use(function error (transition) {
    transition.catch(err => console.error(err.stack))
  })

export default class App extends React.Component {
  render () {
    return (
      <Provider redux={redux}>
        {() => <Router router={cherrytree} />}
      </Provider>
    )
  }
}

function routes (route) {
  let home = ['user', { username: 'KidkArolis' }]
  route('app', { path: '/', redirect: home, component: Application }, () => {
    route('about', { path: 'about', component: About })
    route('stargazers', { path: 'stargazers', component: GithubStargazers }, () => {
      route('repo', { path: ':username/:repo', component: GithubRepo })
      route('user', { path: ':username', component: GithubUser })
    })
  })
}

Note: this example demonstrates how you'd use cherrytree-for-react with redux. If you don't use redux, simply use Router component at the top level, instead of the Provider.

The router will be injected into the context of the render tree. You can use it to generate links or initiate transitions, e.g.

let transition = this.context.router.transitionTo('repo', {username: 'facebook', repo: 'react'})
let url = this.context.router.generate('repo', {username: 'facebook', repo: 'react'})

Browse cherrytree repo for more docs and examples.

Generating Links

<Link> components are used to create an <a> element that links to a route.

Import first

import { Link } from 'cherrytree-for-react'

For example, assuming you have the following route:

route('showPost', {path: '/posts/:postID', component: Post})

You could use the following component to link to that route:

<Link to='showPost' params={{ postId: post.id }} query={{ show: true }} />

To create a link with full (external or local) url, use the href attribute instead

<Link href={`/posts/${post.id}`} />

Server Side Usage

This component can also be used in the server side, in that case, an already started cherrytree instance needs to be passed in, e.g.

// start listening
cherrytree.listen(new cherrytree.MemoryLocation('/foo/bar')).then(function () {
  React.renderToString(<Router router={cherrytree} />)
})

In this case, the <Router> component will detect that the router has already been started and will not call the asynchronous listen function.

For a full, working server side example, see the cherrytree/examples/server-side-example.

Why not a cherrytree middleware?

The typical extension point for cherrytree is the middleware mechanism. However, wrapping cherrytree in a React component is what enables the hot reloading functionality. A new cherrytree instance can be swapped in via the prop into the router during the hot reloads. The router is then kept in the component state meaning we have a reference to the old instance and can clean up using cherrytree.destroy() between the hot reloads. The middleware is still used as a way to update the state of the Router component that triggers the rerender.

Examples

There are currently two examples:

About

Seamlessly use cherrytree router with React

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 97.7%
  • Shell 2.3%