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

rr-named-routes

v1.0.4

Published

A library for using named routes with react-router

Downloads

3

Readme

rr-named-routes

A small library for creating named routes for React Router.

How to use

First, define your routes, each beginning with a forward slash. Use the nested function to define nested routes:

import { nested } from 'rr-named-routes'

export default routes = nested('', {
  users: nested('users', {
    user: nested(':userId', {
      edit: 'edit',
      lists: nested('lists', {
        list: ':listId'
      })
    })
  })
})

This creates the following routes:

/
/users
/users/:userId
/users/:userId/edit
/users/:userId/lists
/users/:userId/lists/:listId

Next wrap your app in the context provider

import { NamedRoutesProvider } from 'rr-named-routes'
import routes from './routes'

const App = () => {
  <NamedRoutesProvider routes={ routes }>
    { children } // <-- Rest of the app....
  </NamedRoutesProvider>
}

Now your routes are available as a hook in any component. Nested routes can be chained, then called as a method with no parameters when defining a route.

import { Router, Route } from 'react-router-dom'
import { useNamedRoutes } from 'rr-named-routes'
...

const AppRoutes = () => {
  const routes = useNamedRoutes()

  return (
    <>
      <Router>
        <Route path={ routes.() } component={ HomeComponent } /> // "/"

        <Route path={ routes.users() } component={ ListUsersComponent } /> // "/users"

        <Route path={ routes.users.user() } component={ ShowUserComponent } /> // "/users/:userId"

        <Route path={ routes.users.user.lists.list() } component={ UserListComponent } /> // "/users/:userId/lists/:listId"
      </Router>
    </>
  )
}

In any component, you can create a link:

import { Link } from 'react-router-dom'
import { useNamedRoutes } from 'rr-named-routes'

const SomeComponent = () => {
  const routes = useNamedRoutes()

  return (
    <>
      <Link to={ routes() }>Home</Link> // "/"

      <Link to={ routes.users() }>All Users</Link> // "/users"

      <Link to={ routes.users.user({ userId: 1 }) }>Specific User</Link> // "/users/1"

      <Link to={ outes.users.user.lists.list({ userId: 1, listId: 2 }) }>Specific User</Link> // "/users/1/lists/2"
    </>
  )
}

Details

Link generation uses React Router's generatePath method to preserve error messages.

Planned Updates

  • Desperately need to figure out the typings

  • Add the ability to ignore an index. For instance, if you wanted to have routes nested under "/settings", but didn't want an endpoint at "/settings".

    • Syntax should be { __index__: false }
  • Add a utility method which can be called in the command line via npm/npx which will print a list of the defined routes.

  • Accept a nested routes definition with '/' at the beginning or end of the routes

  • Allow adding query string parameters

    • Define them on each route, but only have them added if the parameters are passed in. Must be stripped if a nested route is accessed

    • Should be able to accept arbitrary extra params which get appended as query strings in addition to being able to define them in the route definition. For instance, if { path: 'route?search=:search' } was defined, and a params object was passed as such: { search: 'search_value', extra: 'something }, it should produce "/route?search=search_value&extra=something"