npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cherrytree-for-react

v1.1.8

Published

An adapter for cherrytree router for using it with React

Downloads

5

Readme

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: