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

v2.2.0

Published

A supersmall router for react

Downloads

17

Readme

μRouter

μRouter or pico-router is a very lightweight React router with zero fat. It was created to do routing, and only do routing. The use of pico-router is fairly easy for any new React beginner.

Make sure you can reference the source file (router.react.js) by importing it, extend it by creating a new router object that has a defineRoutes method and a getDefaultRoute method, inject it into your main application and create an event listener.

Howto Use

Step by step on how to implement react-pico-router into your project.

Extend the base Router

Extend the Base Router like you would do with any ES6 class.

import Router from 'react-pico-router';

export default class GrandRouter extends Router
{
    
}

Add a defineRoutes method

The defineRoutes method will statically store an array in which all routes should be defined. The structure of this array is simple by design. For each item, the key holds the route you want to use and the value will have an instance of the React Component you want to display when a user visits that URL.

In the defineRoutes, you will be able to pass all the query parameters or only a selection to the React Component, like you would do with any data in React, by injecting it in the props.

You can call the methods getQueryParams() to get all query params and store them in an array, or use the getQueryParam(name) method to specify which variable the view needs.

Remember to only apply changes to the this.routes parameter.

import Router from 'react-pico-router';

export default class GrandRouter extends Router
{
    defineRoutes()
    {
        this.routes['index'] = (<Index />);
        this.routes['search'] = (<Search query={this.getQueryParams()} />;
        this.routes['profile'] = (<UserProfile id={this.getQueryParam(userId)} />);
    }
}

getDefaultRoute method

The getDefaultRoute method you need to implement will just need to return the default React Component that will be used. You can use the Router's this.getRoute() method with a route as parameter to make this pain free.

import Router from 'react-pico-router';

export default class GrandRouter extends Router
{
    defineRoutes()
    {
        this.routes['index'] = (<Index />);
        this.routes['search'] = (<Search query={this.getQueryParams()} />;
        this.routes['profile'] = (<UserProfile id={this.getQueryParam(userId)} />);
    }
    
    getDefaultRoute()
    {
        return this.getRoute('index');
    }
}

Inject into your Main

Define the router globally (viewport as parameter) and inject it into your main application. Then after defining your main application, initiate your Main, by calling the router's getDefaultRoute function.

To see an example of this, you can refer to the example/Main.js

Create the event listener

As a last step, you will need to listen to changes to the url. Use the following snippet in your Main's constructor, or implement something without jQuery

window.addEventListener('hashchange', function() {
    router.navigate()
});