Giter Site home page Giter Site logo

cherrytree-for-react's Introduction

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 react cherrytree cherrytree-for-react
import React from 'react'

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

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

const cherrytree = createCherrytree().map(routes)

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

function routes (route) {
  route('app', { path: '/', 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 })
    })
  })
}

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:

cherrytree-for-react's People

Contributors

kidkarolis avatar nhunzaker avatar oliverwoodings avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cherrytree-for-react's Issues

Peer Dependency

Tried to install with react 0.14 today and had a peer dependency issue. Used npm 3 to get round it for now.

Pass down child routes using context instead of props

Right now if you want to provide additional props to a child route you need to clone it, leading to code like this:

class App extends Component {
  render () {
    return (
      <div>
        {React.cloneElement(this.props.children, { anotherProp: 'foo' })}
      </div>
    )
  }
}

It would be much nicer IMO to have a <RouteHandler /> component like they do in react-router:

class App extends Component {
  render () {
    return (
      <div>
        <RouteHandler anotherProp='foo' />
      </div>
    )
  }
}

This should be pretty easy to achieve. The RouteHandler component would look something like this:

class RouteHandler extends Component {
  static childContextTypes = {
    routes: PropTypes.array
  }
  static contextTypes = {
    routes: PropTypes.array
  }

  getChildContext () {
    return {
      routes: routes.slice(1)
    }
  }

  render () {
    return this.context.routes[0]
  }
}

Thoughts @KidkArolis ?

Updates are being triggered on transition start, not end

Right now a piece of middleware is installed that updates the Router component on transition start. This causes the component to be out of sync with the actual router. The initial app render can be used to demonstrate this:

  1. Router component is passed to ReactDOM.render
  2. Router.componentWillMount method installs middleware then starts the router
  3. Router.render method returns nothing since router has not finished starting (no routes to render)
  4. Initial page transition starts, causing middleware to be called
  5. Middleware updates Router state, causes re-render
  6. Child component renders, tries to read this.context.router.state.routes to get active routes but gets undefined because router is in the middle of a transition.

This is just one example of the inconsistencies; other issues occur if the transition is cancelled (the next state will be rendered upon transition start, but upon cancel won't get reverted to the previous state).

I think this could be solved by using transition.then in the middleware, however we'd have to be careful since if the Router was unmounted mid-transition, we would end up calling setState on an unmounted component.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.