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-pather

v2.0.1

Published

Package for working with named routes in React

Downloads

16

Readme

react-pather

Package for managing named routes in React projects.

NPM JavaScript Style Guide

Install

npm install --save react-pather

Usage

Define two route maps. One for front routes, another one for back routes. Every route name must be unique and can not be equall to reserved words path, sub or isSection.

routes.js

// Route names must be unique and start with /
const frontRoutes = {
    profile: {
        path: '/profile',
        sub: {
            test: {
                path: '/test',
                sub: {
                    v1: '/v1/:id',
                    v2: '/v2/:id',
                }
            }
        }
    },
    view: {
        path: '/view',
        sub: {
            product: '/product/:id',
            user: '/user/:id',
        }
    },
    settings: '/settings'
}

const backRoutes = {
    api: {
        path: '/api/v1',
        sub: {
            product: {
                path: '/product',
                isSection: true,
                sub: {
                    code: {
                        path: '/:code',
                        sub: {
                            deactivate: '/deactivate',
                            addToCart: '/add-to-cart',
                        }
                    }
                }
            },
            account: {
                path: '/account',
                isSection: true,
                sub: {
                    settings: {
                        path: '/settings',
                        isSection: true,
                        sub: {
                            general: '/general',
                            statistics: '/statistics'
                        }
                    }
                }
            }
        }
    }
}

export {
    frontRoutes,
    backRoutes,
};

Create Pather object and pass to the construstor two parameters. The first one is a Front Route Map (FRM) and the second one is a Back Route Map (BRM). Pass Pather object as a value to a PatherProvider.

App.js

import React from 'react'
import Router from './Router';
import { PatherProvider, Pather } from 'react-pather'
import { frontRoutes, backRoutes } from './routes';

const pather = new Pather(frontRoutes, backRoutes);

const App = () => {
  return (
    <PatherProvider value={pather}>
      <Router />
    </PatherProvider>
  );
}

export default App

Usage

There are two ways to access Pather object in a component tree using:

  • usePather hook
  • withPather HOC component

Each of them gives the same functionality, so choose which one is preferable for you. All front routes are accessible via front field of the Pather object, while all back routes can be taken through the back field.

Use via HOC

Router.js

import React from 'react'
import { withPather } from 'react-pather'

function Router({ pather }){
    return (
        <span>
            Profile: {pather.front.profile} <br/>
            Test: {pather.front.test} <br/>
            Product: {pather.back.product} <br/>
            Product Code: {pather.back.Product.code} <br/>
            Product Deactivate: {pather.back.Product.deactivate} <br/>
            Product Add to Cart: {pather.back.Product.addToCart} <br/>
            Account: {pather.back.account} <br/>
            Account Settings: {pather.back.Account.settings} <br/>
            Account Settings General: {pather.back.Account.Settings.general} <br/>
            Account Settings Statistics: {pather.back.Account.Settings.statistics} <br/>
            With params: {pather.reverse(pather.front.v1, { id: '12345' })} <br/>
            Current path: {pather.current} <br/>
            Current path: {pather.location.pathname} <br/>
            Query: {pather.query.toString()}
        </span>
    );
}

export default withPather()(Router);

Use via React hook

Router.js

import React from 'react'
import { usePather } from 'react-pather'

function Router(){

    const pather = usePather();

    return (
        <span>
            Profile: {pather.front.profile} <br/>
            Test: {pather.front.test} <br/>
            Product: {pather.back.product} <br/>
            Product Code: {pather.back.Product.code} <br/>
            Product Deactivate: {pather.back.Product.deactivate} <br/>
            Product Add to Cart: {pather.back.Product.addToCart} <br/>
            Account: {pather.back.account} <br/>
            Account Settings: {pather.back.Account.settings} <br/>
            Account Settings General: {pather.back.Account.Settings.general} <br/>
            Account Settings Statistics: {pather.back.Account.Settings.statistics} <br/>
            With params: {pather.reverse(pather.front.v1, { id: '12345' })} <br/>
            Current path: {pather.current} <br/>
            Current path: {pather.location.pathname} <br/>
            Query: {pather.query.toString()}
        </span>
    );
}

export default Router;

License

MIT © masterr314