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

react-router-addons-routes

v0.0.3

Published

Route configuration conventions for React Router.

Downloads

40

Readme

react-router-addons-routes Travis npm package

react-router-addons-routes Centralized route configuration conventions and components for React Router.

Installation

Using npm:

$ npm install --save react-router-addons-routes

Then with a module bundler like webpack, use as you would anything else:

// using an ES6 transpiler, like babel
import { NamedLink } from 'react-router-addons-routes'

// not using an ES6 transpiler
var NamedLink = require('react-router-addons-routes').NamedLink

The UMD build is also available on unpkg:

<script src="https://unpkg.com/react-router-addons-routes/umd/react-router-addons-routes.min.js"></script>

You can find the library on `window.ReactRouter.addons.Routes

Motivation

With the introduction of React Router v4, there is no longer a centralized route configuration. There are some use-cases where it is valuable to know about all the app's potential routes such as:

  • Loading data on the server or in the lifecycle before rendering the next screen
  • Linking to routes by name
  • Static analysis

This project seeks to define a shared format for others to build patterns on top of.

We Need Help Here

We aren't particularly interested in using this ourselves, which runs the risk of us not prioritizing this project. It is a valuable piece of UI routing that deserves some strong patterns by people who rely on it.

This is meant as a starting point for folks out there who are very interested in it, who will quickly take ownership of it :D

Route Configuration Shape

Routes are objects with the same properties as <Match> with the addition of routes for sub routes and name. Also, consumers are free to add any additional props they'd like.

const routes = [
  { name: 'root',
    component: Root,
    routes: [
      { pattern: '/',
        exactly: true,
        name: 'home',
        component: Home
      },
      { pattern: '/child/:id',
        name: 'child',
        component: Child
      }
    ]
  }
]

API

matchRoutesToLocation(routes, location)

Returns an object containing the following properties

  • matchedRoutes, an array of routes that match the location
  • params, an object of URL parameter names and matching values
const location = { pathname: '/brad-pitt/is/my-cousin' }
const { matchedRoutes, params } = matchRoutesToLocation(routes, location)

// now you could do some sort of data loading
// lets assume the route's components have a `loadData` static
// function on them:
Promise.all(
  matchedRoutes.filter(route => route.component.loadData).map(route => (
    route.loadData(params)
  ))
).then(data => {
  // put the data somewhere and render
})

This can be used server-side or in a data component's lifecycle to determine which routes are going to be rendered next. Ideal for data loading.

<RoutesProvider routes>

Puts your routes on context so other components can work with them.

ReactDOM.render((
  <RoutesProvider routes={routes}>
    <App/>
  </RoutesProvider>
), el)

<MatchWithRoutes>

A sub-routes aware replacement for <Match>. Render these instead and they will pass down the sub routes to the next rendered route component.

const App = ({ routes }) => (
  <BrowserRouter>
    <RoutesProvider routes={routes}>
      <div>
        <h1>App</h1>
        {routes.map(route => <MatchWithRoutes {...route}/>)}
      </div>  
    </RoutesProvider>
  </BrowserRouter>
)

const Child = ({ routes }) => (
  <div>
    <h2>Child</h2>
    {routes.map(route => <MatchWithRoutes {...route}/>)}
  </div>  
)

//////////////////////////////////////////////////////////
const routes = [
  { pattern: '/',
    name: 'root',
    component: Root,
    routes: [
      { pattern: '/child/:id',
        name: 'child',
        component: Child
      }
    ]
  }
]

ReactDOM.render(<App routes={routes}/>, el)

<NamedLink to params>

Links to routes by name.

const routes = [
  { name: 'user',
    pattern: '/users/:id'
  }
]

<NamedLink to="user" params={{ id: user.id }}>
  {user.name}
</NamedLink>

<NamedRedirect>

Not implemented.