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

mr-router5

v7.0.0-beta.1

Published

mr-router5 uses Router5 together with MobX for your favorite React application.

Downloads

60

Readme

mr-router5   npm version npm bundle size

mr-router5 uses Router5 together with MobX for your favorite React application.

  • router5 is an awesome framework and view library agnostic router.
  • MobX is a battle tested library that makes state management simple and scalable by transparently applying functional reactive programming.

Why mr-router5

mr-router5 creates a minimal bridge between router5, MobX and React. When I say 'minimal bridge', I really mean it. Your React components stay untouched, there's no need to create HOC for your root App component. The route view features makes the route transition more flexible and powerful.

Peer Dependencies

  • mobx: ^6,
  • mobx-react-lite: ^3 || ^4,
  • react: >=16.8,
  • router5: ^7 || ^8

Installation

npm install --save mr-router5

Basic Usage

Integrating mr-router5 into your React application is straightforward. Below is an example that demonstrates the essential steps:

  1. Create Route Node Components: Start by creating the route node components.

see router5 docs for the core concepts.

export const RootNode = () => (
    <div>
        <h2>Site Header</h2>
        {/*
            IMPORTANT: The 'routeNodeName' prop must match the route node name defined in step 2!
            (empty string for root route node)
        */}
        <RouteComponent routeNodeName="" />
        <h4>Footer</h4>
    </div>
);

export const UserNode = () => (
    <div>
        <h2>User Page</h2>
        <RouteComponent routeNodeName="users" />
    </div>
);
  1. Set Up Route Views: mr-router5 utilizes RouteView to associate routes with components. Here, we define a set of route views, linking each route to its respective component.
import { RouteView } from "mr-router5";
// import route nodes and other components

const routeViews = [
    new RouteView({name: "home", path: "/"}, Home),
    new RouteView({name: "users", path: "/users"}, UserNode),
    new RouteView({name: "users.list", path: "/list"}, UserList),
    new RouteView({name: "users.view", path: "/view"}, UserView),
];

Note: mr-router5 currently supports only flat routes, meaning nested routes are not yet available.

  1. Initialize and start the Router: Create and initialize the router with the defined route views. This sets up the routing logic for your application. Finally start the router to enable route handling in your application.

see router5 docs for adding options and dependencies.

const options = {};
const dependencies = {};
const router = routerStore.createRouter(routeViews, options, dependencies);
router.start(() => {
    render(<RootNode />, document.getElementById("app"));
});

Extra Features

You can add extra functionalities to route views by attaching the extra and dataLoader payload. There are many ways to use them, one example would be middleware. The middleware is ideal for fetching data or checking authentication prior to navigating to the target route. The extra and dataLoader payload will further streamline the original router5 middleware implementation.

see router5 docs for adding middlewares.

  1. Attach Extra and DataLoader: You can attach additional data or loaders to your route views. This is useful for scenarios like data preloading or conditional route handling.
import { RouteView } from "mr-router5";
// import route nodes and other components

const routeViews = [
    new RouteView({name: "login", path: "/login"}, Login),
    new RouteView({name: "user", path: "/user"}, UserComponent)
        .setExtra("user", "John Doe")
        .setExtra("requireLogin", true)
        .setDataLoader("getUserDetail", (user) => ({ /* user details */ }))
];
  1. Middleware Implementation: makeMiddleware() simplifies and adds additional context to the original router5 middleware implementation.
import { makeMiddleware } from "mr-router5";

const middleware = (middlewareData) => {
    // middlewareData is an object that can be destructured with the following properties.
    const {
        router,
        toState,
        fromState,
        done,
        getDataLoader,
        getExtra,
    } = middlewareData;
    
    // skip login check if 'requireLogin' is false.
    if (!getExtra("requireLogin", false)) {
        done();
    }
    else {
        // check if user is logged in
        const isLoggedIn = true;
        if (isLoggedIn) {
            const user = getExtra("user");
            const userDetail = getDataLoader("getUserDetail")(user);
            done();
        }
        else {
            done({ redirect: { name: "login" } });
        }
    }
}

router.useMiddleware(makeMiddleware(middleware));

That's it. Enjoy routing.