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-static-config

v0.2.0

Published

Static route config matching for React Router

Downloads

24

Readme

React Router Static Config

Static route configuration helpers for React Router.

This is a fork off react-router-config with support for partially applied routes (you get to keep your code splitting support) and static switch matching.

Installation

Using npm:

$ npm install --save react-router-static-config

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

import { matchRoutes, createPartialRoutes } from 'react-router-static-config'

/* OR */

import matchRoutes from 'react-router-static-config/matchRoutes'
import createPartialRoutes from 'react-router-static-config/createPartialRoutes'

The UMD build is also available on unpkg:

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

You can find the library on window.ReactRouterStaticConfig

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.

Route Configuration Shape

Routes are objects with the same properties as a <Route> with a couple differences:

  • the only render prop it accepts is component (no render or children)
  • introduces the routes key for sub routes
  • Consumers are free to add any additional props they'd like to a route, you can access props.route inside the component, this object is a reference to the object used to render and match.
  • The name is what will be used as object key when using createPartialRoutes.
import { matchRoutes } from 'react-router-static-config'


const routes = [
  {
	  path: '/',
	  name: 'Home',
	  exact: true,
	  component: Home
  },
  {
	  path: '/child/:id',
	  name: 'Child',
	  component: Child,
	  routes: [
		  {
			  path: '/child/:id/grand-child',
			  name: 'Grandchild',
			  component: GrandChild
		  }
    ]
  }
]


// Matching the Child route
matchRoutes(routes, '/child/jeff')

Static-Config also supports Redirects if you provide a Redirect Object:

const routes = [
	{
		path: '/'
		name: 'Home',
		component: Home,
	},
	{
		path: '/oldhome'
		redirect: {
			to: '/'
			push: true,
		}
	}
]

Note: Just like <Route>, relative paths are not (yet) supported. When it is supported there, it will be supported here.

API

matchRoutes(routes<object>, pathname<string>, useSwitch<bool> = true)

Returns matched route.
Returns an array of matched routes if useSwitch = false.

Parameters

  • routes - the route configuration
  • pathname - the pathname component of the url. This must be a decoded string representing the path.
  • useSwitch - If should use <Switch> behavior. Recurses with child routes first if they are available to find match. This means nested child routes with be matched first when using switch.
import { matchRoutes } from 'react-router-static-config'
const branch = matchRoutes(routes, '/child/23', false)
// using the routes shown earlier, this returns
// [
//   routes[0],
//   routes[0].routes[1]
// ]


// Using matchRoute's switch
import { matchRoutes } from 'react-router-static-config'

const match = matchRoutes(routes, '/child/23/grand-child')

// With switch, this returns
// {
//      path: '/child/:id/grand-child',
//			name: 'Grandchild',
//			component: GrandChild
//  }

createPartialRoutes(routes)

In order to ensure that matching outside of render with matchRoutes and inside of render result in the same branch, you must use createPartialRoutes instead of <Route> inside your components. You can render a <Route> still, but know that it will not be accounted for in matchRoutes outside of render.

import { BrowserRouter, Switch } from 'react-router'
import { createPartialRoutes } from 'react-router-static-config'

const Home = () => (
  <div>
    <h2>Home</h2>
  </div>
)


// Ideally you would put this in another file
const childRoutes = [
  {
    path: '/child/:id/grand-child',
    name: 'Grandchild',
  }
]

const routes = [
  {
	  path: '/',
	  name: 'Home',
	  exact: true,
	  component: Home
  },
  {
	  path: '/child/:id',
	  name: 'Child',
	  routes: childRoutes,
  }
]


const partialRoutes = createPartialRoutes(routes)

const GrandChild = ({ someProp }) => (
  <div>
    <h3>Grand Child</h3>
    <div>{someProp}</div>
  </div>
)

const Child = () => {
    const childRoutes = createPartialRoutes(childRoutes);
    return (
        <div>
          <h2>Child</h2>
          
          <childRoutes.GrandChild component={GrandChild} />
        </div>
    )
}

const Root = () => (
  <div>
    <h1>Root</h1>
    
    <Switch>
	    {/* Since we already declared our component in config we don't need to apply any more props */}
	    <partialRoutes.Home />
	    
	    <partialRoutes.Child component={Child} />
    </Switch>
  </div>
)


ReactDOM.render((
  <BrowserRouter>
    {/* kick it all off with the root route */}
    <Root />
  </BrowserRouter>
), document.getElementById('root'))