Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ryancole/router

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ryancole/router

A simple router for react.

  • 0.0.4
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

A simple router for React.

npm install @ryancole/router

@ryancole/router is a simple router for React. The main focus of the router is to function in a reliable and predictable manner in both client and server environments.

Concepts

The main concept of the router is that routes are an up-front, static resource. The router operates on an array of route objects. You define your routes up front. You can take the requested URL and match it against your routes and get the exact route that should be rendered. Likewise, you can compile route URLs using named routes and route parameters.

A route is intended to be thought of as a complete "page" or "view" to be rendered by React. The intention is that, when the URL pathname changes and this router finds the appropriate React component to render, the component being rendered is going to make up your complete application view so that you can simply call ReactDOM.render on the returned component.

There is no route nesting, like react-router v3, and there are no floating route components, like react-router v4. Because of this, there is a clear place to perform code splitting and asynchronous loading of split bundles.

import {routes} from "@ryancole/router";

export default routes([
  {
    path: "/",
    loadComponent: () => System.import("../client/page/Home")
  },
  {
    path: "/team",
    loadComponent: () => System.import("../client/page/Teams")
  },
  {
    name: "team",
    path: "/team/:slug",
    loadComponent: () => System.import("../client/page/Team")
  },
  {
    path: "*",
    isNotFound: true,
    loadComponent: () => System.import("../client/page/NotFound")
  }
]);

The router will provide the first route object that has a path that matches the requested URL pathname. The path may use express-style parameters.

A route object must specify a loadComponent function that returns the React component to be rendered when this route is matched. loadComponent should return a Promise that resolves to the loaded React component.

import {match} from "@ryancole/router";
import createHistory from "history/createBrowserHistory";

const history = createHistory();

// on route change, re-render
history.listen(renderLocation);

function renderLocation(location) {
  const route = match(location, routes);
  if (route) {
    route.loadComponent(route, history).then(Component => {
      ReactDOM.render(<Component />, destination);
    });
  }
}

// render current route
renderLocation(history.location);

The router will also check to see if the route's component has any data concerns and will fetch the necessary data. A React component can implement a static function called getInitialProps if it needs to have data fetched prior to being rendered. If implemented, the getInitialProps return value object will be provided to the route component as props.

export default function Team({team}) {
  return (
    <Template>
      <h1>{team.name}</h1>
    </Template>
  );
}

Team.getInitialProps = async ({slug}) => {
  const team = await Teams.getBySlug(slug);
  return { team };
}

FAQs

Package last updated on 11 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc