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

router-util

v1.0.4

Published

A small library containing simpler routing APIs for use with react projects that require routing

Downloads

7

Readme

router-util

npm version minified + gzipped

This library contains a few API's that provides routing capabilities for react apps that are multi pages

It provides a component that can be configured with all routes and their components and handles the details of rendering the necessary routes and setting up the underlying mechanism of routing in react apps.

Built with react-router-dom and React

Installation

Install from NPM or Yarn

   npm i router-util --save
   yarn add router-util

Usage

Declare your routes as a list with this format

   //src/routes.js

   import MyComponetn1 from './src/MyComponent1
   import MyComponent2 from './src/MyComponent2

   const routes = [
      {
         path: '/page1',
         component: MyComponent1,
         exact: true,
         key: 'sd'      // unique key
      },
      {
         path: '/page2',
         component: MyComponent2,
         exact: true,
         key: 'sdas'  
      }
   ]

   export default routes 

In App.js

   import React from 'react'
   import {RenderRoutes} from 'router-utils'
   import routes from './routes'

   function App(props){
      return (
         <RenderRoutes routes={routes}>
      )
   }

   export default App

To use Routing capabilities in other components, for example to redirect users once they have signed in to another page. use the useRouter() hook exported from router-util.

In Signin.js.

   import React from 'react'
   import {useRouter} from 'router-util'
   // auth utils 
   import {useAuth} from '../auth'
   import SigninPageComponent from './SigninPageComponent'

   function Signin(props){
      // grab the router object
      const router = useRouter()
      const auth = useAuth()

      const signinUser = () => {
         auth.signin(/*signin*/)
            .then(() => {
               router.push('/page')
            }).catch(console.error)
      }

      return (
         <SigninPageComponent signin={signin} />
      )
   }

Exports

  • RenderRoutes - takes a list of routes, renders a BrowserRouter with the routes provided
  • useRouter() - A hook to allow components use routing capabilities. It exposes a router with the following APII
   {
      push - to push a route to the history
      pop - pops from history 
      state - access route state 
      query - access route query params
      replace - replace a route on the history stack 
      ... react-router-dom props
   }
  • scrollToTop - A utility to scroll the window to the top

Author

github/shaddyshad

License

MIT © shaddyshad