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

@delta62/micro-router

v2.3.0

Published

A tiny router for React

Downloads

31

Readme

micro-router

A tiny router for React apps

This library aims to provide the most useful high-level features of client-side routing without extra bells or whistles. It's about 3kb after minification, and has no dependencies. If you are looking for a Cadillac router with nested routes and other more complex features, something such as react-router may be what you're looking for.

Usage

import { Route, RouteProvider } from '@delta62/micro-router'

let RouteExample = () => (
  <RouteProvider>
    <Route path={['/games/:game', '/']}>
      <App />
    </Route>
    <Route path="/login">
      <LoginPage />
    </Route>
    <Route path="/signup">
      <SignUpPage />
    </Route>
  </RouteProvider>
)

Routes are added in the component tree under a RouteProvider, which takes care of synchronizing the browser's location state with your components. This library is fully integrated with the history API, so pressing the back button after switching routes will invoke previous routes rather than exiting your app.

You can change the current location either programatically via a React context which RouteProvider provides, or declaratively with embedded components.

<RouteProvider>, RouteContext

This context holds navigation data which can be used in your application. The context object holds the following properties:

{
    params: Record<string, string>,
    path: string,
    setPath(newPath: string): void,
    setRouteParams(newParams: Record<string, string>): void
}

It's recommended that you use the included components to update the context, but of course it's possible to access directly for programmatic changes via React.useContext().

<Route path={string | string[]}>

This component only renders its children when the given path is currently active. If one component should match multiple URLs, you can provide an array of paths to match.

The route matching syntax is very minimal - you can include either hard- coded strings or route parameters, which are prefixed with a :.

/path/to/resource
/person/:name

Optional components are also supported. It only makes sense to use these at the end of your route definition; putting them anywhere else would create a route that only matches when all of the optional parameters are present. Denote parameters as optional by prefixing them with a ?.

/resource/?name
/root/?subView/?subSection

Leading and trailing slashes are optional in both the route definition and the path given from the browser at runtime.

<Anchor href={string}>

Renders an HTML <a> tag which is implicitly hooked up to the router's context, invoking micro-router instead of the browser's default navigation behavior. Anchors that are currently active (their destination matches the current location) have an active class added to them.

<Redirect to={string} when={boolean?}>

Redirects the user to a different location (to) when a given condition (when) is met. The condition is optional, and defaults to true (if the component is rendered, by default it will always redirect). This component does not generate any markup.