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

lumbridge-router

v0.1.3

Published

Improved logic management for React apps.

Downloads

7

Readme

lumbridge-router

🏰 React application management made simple.

npm GitHub react

Installation

Using npm:

npm i --save lumbridge-router

Using yarn:

yarn add lumbridge-router

Then import the helper classes where needed.

import { Router } from 'lumbridge-router';

const router = Router.create({
  // code...
});

API

Config

Each router is configured with a config object:

const config = {
  // options...
};

const router = Router.create(config);

This config object will contain all the information required by the router.

config.routes

  • Type: object
  • Required: true

A collection of routes which may be rendered by the router relative to the app's url.

const router = Router.create({
  routes: {
    // routes...
  },
});

Example:

const router = Router.create({
  routes: {
    home: {
      path: '/',
      exact: true,
      component: HomePage,
    },
    dashboard: {
      path: '/dashboard'
      component: Dashboard,
      enter: {
        before: () => userIsLoggedIn(),
      },
      leave: {
        after: () => sendGoodbyeMessage(),
      },
    },
  },
});

Properties:

  • routes[routeName].path [string]: the path which will be checked against the url.
  • routes[routeName].component [node]: a React component which will be shown when this route is rendered.
  • routes[routeName].exact [node]: specifies that this route must match exactly to the url.
  • routes[routeName].enter.before [func]: handler called before the route is rendered. Returning false to this will stop the route rendering and is a good place to put route guards.
  • routes[routeName].enter.after [func]: handler called after the route has been rendered.
  • routes[routeName].leave.before [func]: handler called before the route is rendered. Returning false to this will stop the route from changing.
  • routes[routeName].leave.after [func]: handler called after the route has been rendered.

Troubleshooting:

  • Only one route will be shown at a time for a single router (but you may use multiple routers).
  • The route with a path most similar to the actual route will be rendered.
  • Routes with the exact key will be matched only when the location exactly matches the path key.
  • Each route has a key (e.g. config.routes.home) which may be used to identify the route.

config.change

  • Type: object
  • Required: false

A collection of handlers which will be called when the router renders a new route.

const router = Router.create({
  change: {
    // handlers...
  },
  routes: {
    // code...
  },
});

Example:

const router = Router.create({
  change: {
    before: () => console.log('The route is about to change.'),
    after: () => console.log('The route has changed.'),
  },
  routes: {
    // code...
  },
});

Properties:

  • change.before [func]: handler called before the route changes. Returning false to this will prevent the route from changing.
  • change.after [func]: handler called after rendering a route.

config.nomatch

  • Type: object
  • Required: false

These values are used when no routes are matched by the routes.

const router = Router.create({
  nomatch: {
    // redirect...
  },
  routes: {
    // code...
  },
});

Example:

const router = Router.create({
  nomatch: {
    redirect: '/not-found',
  },
  routes: {
    // code...
  },
});

Properties:

  • nomatch.redirect [node]: the url which the app will redirect to when no route matches found.

Usage

router.compile

  • Type: node (React component)

A React component which is used to display the routes in the DOM.

const router = Router.create({
  routes: {
    // code...
  },
});

const Routes = router.compile();

const App = () => (
  <div>
    <h1>My Cool App</h1>
    <div>
      <Routes />
    </div>
  </div>
);

Link

  • Type: node (React component)

A React component which let's you link to your routes.

import { Link } from 'lumbridge'; // or 'lumbridge-router'

const Menu = () => (
  <Wrap>
    <Link to="/">Home</Link>
    <Link to="/faq">FAQ</Link>
    <Link to="/about">About</Link>
  </Wrap>
);

Nested Routes

The router does not provide functionality for nested routes.

✋ Hold up! How could this be?...

We made this decision on purpose to avoid unnecessary complexity. If you would like to use nested routes, simply create multiple route components and put those routes inside the components rendered by other routers.

Example:

const ChildRoutes = Router.create({
  routes: {
    nested: {
      path: '/main/nested',
      component: NestedComponent,
    },
  },
}).compile();

const NestedComponent = () => (
  <InnerWrap>
    <ChildRoutes />
  </InnerWrap>
);

const ParentRoutes = Router.create({
  routes: {
    nested: {
      path: '/main',
      component: NestedComponent,
    },
  },
}).compile();

const App = () => (
  <OuterWrap>
    <ParentRoutes />
  </OuterWrap>
);

Packages