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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@yusuke-suzuki/rize-router

v0.1.3

Published

A routing library for React SPA with simple auth.

Readme

rize-router

A routing library for React SPA with simple auth, powered by:

Install

yarn add @yusuke-suzuki/rize-router

Quick Start

// index.js
import React from 'react';
import ReactDOM from 'react-dom';

import { StoreContext } from 'redux-react-hook';
import configureStore from './configureStore';

import { createBrowserHistory } from 'history';
import { RouterContext } from '@yusuke-suzuki/rize-router';

import App from './App';

const { store } = configureStore();
const history = createBrowserHistory();

ReactDOM.render(
  <StoreContext.Provider value={store}>
    <RouterContext.Provider value={history}>
      <App />
    </RouterContext.Provider>
  </StoreContext.Provider>,
  document.getElementById('root')
);
// App.js
import React, { useCallback } from 'react';
import { useMappedState } from 'redux-react-hook';

import { Router } from '@yusuke-suzuki/rize-router';
import routes from './routes';

import Login from './pages/Login';
import NotFound from './pages/NotFound';

const App = () => {
  const mapState = useCallback(
    state => ({
      user: state.auth.user
    }),
    []
  );
  const { user } = useMappedState(mapState);

  return (
    <Router
      routes={routes}
      fallback={{
        path: '',
        component: NotFound
      }}
      authFallback={{
        path: '/login',
        component: Login
      }}
      authenticated={user ? true : false}
    />
  );
};

export default App;
// routes.js
import Books from './pages/Books';
import BookInfo from './pages/BookInfo';
import Login from './pages/Login';

const routes = [
  {
    path: '/',
    component: Books,
    requireAuth: true
  },
  {
    path: '/books',
    component: Books,
    requireAuth: true
  },
  {
    path: '/books/:id',
    component: BookInfo,
    requireAuth: true
  },
  {
    path: '/login',
    component: Login,
    requireAuth: false
  }
];

export default routes;

Usage

RouterContext

Before you can use the router, you must provide the history object via RouterContext.Provider:

import { createBrowserHistory } from 'history';
import { RouterContext } from '@yusuke-suzuki/rize-router';

const history = createBrowserHistory();

ReactDOM.render(
  <RouterContext.Provider value={history}>
    <App />
  </RouterContext.Provider>,
  document.getElementById('root')
);

Router

routes: array

An array of route object. Each route object have following properties:

  • path: Any valid URL path that path-to-regexp understands.
  • component: A React component to be rendered.
  • requireAuth: Whether the route requires authentication. If requireAuth is set to true and the current value of authenticated is false, it will be redirected to the route specified in authFallback.

fallback: object (optional)

An object that have following properties:

  • path: Any valid URL path that path-to-regexp understands. If the corresponding path is not defined in routes, it will be pushed to this path as fallback.
  • component: A React component to be rendered. If the corresponding path is not defined in routes, this component will be rendered as fallback.

authFallback: object (optional)

An object that have following properties:

  • path: Any valid URL path that path-to-regexp understands.
  • component: A React component to be rendered.

authenticated: bool (optional)

The current authentication state in your application. Required if using authFallback.

onLocationChange: func (optional)

You can listen for changes to the current location using onLocationChange:

import React, { useCallback } from 'react';

import { Router } from '@yusuke-suzuki/rize-router';
import routes from './routes';

const App = () => {
  const handleLocationChange = useCallback(location => {
    console.log(location);
  }, []);

  return <Router routes={routes} onLocationChange={handleLocationChange} />;
};

export default App;

props.params

Each React component defined in routes can receive parameters from path via props.

import React from 'react';

const BookInfo = props => {
  console.log(props.params); //=> { id: '100' } }

  return <div>Book Info</div>;
};

Link

Provides declarative, accessible navigation around your application.

import React from 'react';
import { Link } from '@yusuke-suzuki/rize-router';

const Books = () => {
  return <Link to="/books/100">MyBook 100</Link>;
};

With Material-UI

import React from 'react';
import { Link } from '@yusuke-suzuki/rize-router';
import Button from '@material-ui/core/Button';

const Books = () => {
  return (
    <Button component={Link} to="/books/100">
      MyBook 100
    </Button>
  );
};

useHistory

Simply returns the history object.

import React, { useCallback } from 'react';
import { useHistory } from '@yusuke-suzuki/rize-router';

const Books = () => {
  const history = useHistory();

  const handleBookClick = useCallback(
    bookId => {
      history.push(`/books/${bookId}`);
    },
    [history]
  );

  return <Button onClick={() => handleBookClick(100)}>MyBook 100</Button>;
};

Modal Route

You can create a modal route using location.state.modal.

If you call history.push with state: { modal: true }, Router will render the previous route.

This means that you can open your dialogs without re-rendering the page, also switch to the location specified in pathname.

If you visit the site directly and there is no previous route, Router will render the route that matches the current path, regardless of whether state.modal is specified.

  1. Call history.push or using Link with state.modal:
import React, { useCallback } from 'react';
import { useHistory } from '@yusuke-suzuki/rize-router';

const Books = () => {
  const history = useHistory();

  const handleBookClick = useCallback(
    bookId => {
      history.push({
        pathname: `/books/${bookId}`,
        state: {
          modal: true,
          book: myBook
        }
      });
    },
    [history]
  );

  return <Button onClick={() => handleBookClick(100)}>MyBook 100</Button>;
};

export default Books;
  1. Open / Close your dialog on location changed:
import React, { useMemo, useEffect, useCallback } from 'react';
import { useHistory } from '@yusuke-suzuki/rize-router';
import { match } from 'path-to-regexp';

import Dialog from '@material-ui/core/Dialog';

const BookInfoDialog = () => {
  const [dialogOpen, setDialogOpen] = useState(false);
  const [currentBook, setCurrentBook] = useState(undefined);

  const history = useHistory();

  const unlisten = useMemo(() => {
    return history.listen(location => {
      const matched = match('/books/:bookId')(location.pathname);

      if (matched && location.state && location.state.modal) {
        setCurrentBook(location.state.book);
        setDialogOpen(true);
      } else {
        setDialogOpen(false);
      }
    });
  }, [history]);

  const handleRequestDialogClose = useCallback(() => {
    history.goBack();
  }, [history]);

  useEffect(() => {
    return () => {
      unlisten();
    };
  }, [unlisten]);

  return (
    <Dialog open={dialogOpen} onClose={handleRequestDialogClose}>
      This is book info dialog.
    </Dialog>
  );
};