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

v0.1.3

Published

Declarative browser routing for React Web applications

Downloads

4

Readme

react-router-slim

Declarative browser routing for React Web Applications.

Routing rules are declared using Router and Route components.

Router

Router is a root component that declares a route dependent part of an application.

Router props:

  • navigate - method for navigation between routers;
  • match - method that matches current path with path argument and returns the result with matched params;
  • changeEvent - name of an event that may change the current path. Router listens for this event and updates content when the event is fired;
  • getCurrentPath - method for retrieving the current path;

Default implementation relies on the browser History API for the frontend routing, so default implementation of navigate is window.history.pushState or window.history.replaceState, changeEvent is popstate, and getCurrentPath returns window.location.pathname.

Default implementation of the match function uses the pathToRegexp function from the path-to-regexp library https://github.com/pillarjs/path-to-regexp. All rules for the path argument of pathToRegexp function work for the path prop value of a child Route component.

Router provides the RouterContext to children. RouterContext contains only two functions: match and navigate from the Router component. Match is usually used by child Route components. Navigate is used by children to trigger transitions between routes.

Route

Route is a child component of the Router that controls rendering of its children depending on the current router path.

Route props:

  • path - path which Route renders children for;
  • error - component that is rendered in case of any errors in route children;

Route provides the RouteContext to children. RouteContext properties are:

  • path - current router path;
  • params - template parameters of a route;
  • matches - all path matches (may contains more than one in case of overlapped routes);
  • error - error raised by one of children;

If Route's paths are overlapped then both are rendered.

The default browser history routing strategy can be replaced with custom routing strategy (hash, memory, etc.) via Router component props: match, navigate, changeEvent, getCurrentPath.

It does not provide server side routing. You can use the routing provided by your back-end API for this.

Examples

Our examples also demonstrate how to implement URL hash routing.

Simple

const App = () => <Router>
    <Route path="/(.*)">
        <Route path="path1">{child1}</Route>
        {/* "path2/:param" and "path2/(.*)" are overlapped, both are rendered */}
        {/* this component has params.param === ...rest of path... */}
        <Route path="path2/:param">{child21}</Route>
        {/* this component has params[0] === ...rest of path... */}
        <Route path="path2/(.*)">{child22}</Route>
        <Route path="path3/(.*)">{child3}</Route>
        {/* this is a fallback route */}
        <Route>{fallback}</Route>
    </Route>
</Router>;

More complicated

The source codes are in the repository https://github.com/only-jsx/react-router-slim-examples

//index.tsx
import React from 'react'
import { Router, Route, RouterContext, RouteContext } from 'react-router-slim';

interface LinkProps extends React.PropsWithChildren {
    to: string;
}

//Router Link component implementation
const Link = ({ children, to, replace }: LinkProps) => {
    const router = React.useContext(RouterContext);
    const onClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
        e.preventDefault();
        router.navigate?.(to, undefined, replace);
    };

    return <a href={to} onClick={onClick}>{children}</a>
}

//It uses Route paramaters
const RoutedSpan = ({ children }) => {
    const route = React.useContext(RouteContext);

    if (!route) {
        throw new Error('RoutedSpan is not allowed outside the Router component');
    }

    const { params } = route;
    return <span>{children} {params.toString()}</span>;
}

//It does navigation to other routes
const LinkButton = ({ children }) => {
    const router = React.useContext(RouterContext);

    if (!router.navigate) {
        throw new Error('Router navigate is not defined');
    }

    return <button onClick={(e) => router.navigate('/hello/world', {state: 0}, true)}>{children}</button>;
}

const App = () => <Router>
    {/* Regular expressions in path */}
    <Route path="/router/(.*)">
        <Layout />
        {/* Part of a parent route path before any named parameter or regexp */}
        {/* becames a prefix for a child route */}
        {/* so this actually matches to /router/home */}
        <Route path="home">
            <RoutedSpan>RoutedSpan</RoutedSpan>
            <LinkButton>LinkButton</LinkButton>
            <Link to="/hello/world">Link</Link>
        </Route>
        <Route path="await"><AwaitPage/></Route>
        <Route path="long-load"><LongLoad/></Route>
        <Route path="todos" error={ErrorBoundary}>
            <TodosList/>
        </Route>
        <Route path="todos/(.*)" error={ErrorBoundary}>
            <h5>Todo</h5>
            {/* Named parameters */}
            <Route path=":id"><Todo/></Route>
        </Route>
        <Route path="error" error={ErrorBoundary}><ErrorComponent/></Route>
        {/* Route without a path is a fallback route */}
        <Route><Fallback /></Route>
    </Route>
</Router>;