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-micro-router

v1.7.0

Published

Bare-bones router for React

Downloads

94

Readme

react-micro-router

NPM version Build Status

Bare-bones router for React

Features

  • Very minimal (~ 200 loc)
  • Solid and uncomplicated for simple use-cases
  • Declarative API like react-router

Installation

npm i -S react-micro-router

Usage

import React from 'react';
import {Route, Link} from 'react-micro-router';
import {Home, Hello} from './components';

export default function App() {

    return (
        <div>
            <Link to="/">Home</Link>
            <Link to="/hello">Hello</Link>

            <Route path="/" exact>
                <Home/>
            </Route>

            <Route path="/hello">
                <Hello/>
            </Route>
        </div>
    );

}

Nest routes

<Route path="/">
    <Route path="/fruit">
        <h1>Fruit</h1>
        <Route path="/fruit/apples">
            <h2>Apples</h2>
        </Route>
        <Route path="/fruit/oranges">
            <h2>Oranges</h2>
        </Route>
    </Route>
</Route>

Use regular expressions

<Route path="/">
    <AlwaysHere/>
    <Route path="/($|other)">
        <RootOrOther/>
    </Route>
    <Route path="/things" exact>
        <Link to="/things/1">Thing 1</Link>
        <Link to="/things/two">Thing Two</Link>
    </Route>
    <Route path="/things/([0-9a-z]+)">
        <Thing/>
        <Link to="/things">Back</Link>
    </Route>
</Route>

Get current path and regex capture groups

function MyComponent(props) {
    const {path, params} = props.route;
    return (
        <div>
            <p>{path}</p>
            <p>{params[0]}</p>
        </div>
    );
}

<Route path="/hello/([a-z]+)">
    <MyComponent/>
</Route>

// for location.pathname `/hello/world` outputs:

<div>
    <p>/hello/([a-z]+)</p>
    <p>world</p>
</div>

TypeScript

Use ComponentRouteProps to get access to path and params in your component when using TypeScript.

import {ComponentRouteProps} from 'react-micro-router';

type Props = {
    text: string;
    route: ComponentRouteProps
}

function MyComponent(props: Props) {
    const {path, params} = props.route;
    return (
        <div>
            <p>{path}</p>
            <p>{params[0]}</p>
            <p>{props.text}</p>
        </div>
    );
}

Note: when calling the component Typescript will complain about the props not being present. To avoid this, either allow an undefined route prop:

type Props = {
  text: string;
  route?: ComponentRouteProps;
}

Or use a function as a child to render child components:

function Router(props) {
  
    return (
        <Route>
          {(route) => <ChildComponent route={route} otherProp={false} />}
        </Route>
    );
}

Transitions (see react-transition-group for details)


const transition = {
    name: 'Transition',
    appear: true,
    appearTimeout: 400,
    enter: true,
    enterTimeout: 600,
    leave: true,
    leaveTimeout: 400
};

function App() {
    return (
        <Route className="App" path="/">
            <Header/>
            <Route path="/" exact transition={transition}>
                <Oh/>
            </Route>
            <Route path="/yes" transition={transition}>
                <Yes/>
            </Route>
            <Footer/>
        </Route>
    );
}
.Transition-enter {
    opacity: 0;
}

.Transition-enter.Transition-enter-active {
    opacity: 1;
    transition: opacity 400ms ease-in 200ms;
}

.Transition-leave {
    opacity: 1;
}

.Transition-leave.Transition-leave-active {
    opacity: 0;
    transition: opacity 400ms ease-out;
}

.Transition-appear {
    opacity: 0;
}

.Transition-appear.Transition-appear-active {
    opacity: 1;
    transition: opacity 400ms ease-in;
}

Link supports activeClassName (it defaults to active).

<Link to="/hello" activeClassName="is-active">Hello</Link>
// for route /hello renders <a class="is-active">Hello</a>

<Link to="/" className="Link" activeClassName="current">Home</Link>
// for route / renders  <a class="Link current">Home</a>

Control link active state using Regex

// links to / but will be active for / and /also
<Link to="/" match="/($|also)">Hello</Link>

Get current path and regex capture groups from router

import {getCurrentPath, getParams} from 'react-micro-router';

// example: location.pathname /foo/bar/42
const path = getCurrentPath(); // '/foo/([a-z]+)/([0-9]+)'
const params = getParams(); // ['bar', '42']