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

stateside

v1.4.5

Published

Super simplified router implementation for React.

Downloads

30

Readme

Stateside: A React Router

There are a number of great routing solutions for React, however, sometimes you just want a router that works without a bunch of set up or special rules. That's where this library comes in.

Usage

Install via npm:

npm install --save stateside

Add Some Routes

This library exposes a special <Router> component that allows you to quickly hook up your existing components to the window's location.

Let's demonstrate how routing works by adding a home and about page. Just create the components you want to use for each page and add a route prop with the desired pattern you want the URI to match.

import * as React from "react";
import { render } from "react-dom";
import { Router } from "stateside";

function Home () {
  return <h1>Home</h1>
}

function About () {
  return <h1>About</h1>
}

render(
  <Router>
    <Home route="/" />
    <About route="/about" />
  </Router>
  , document.getElementById("app")
);

Add a Default Route

Often times, you need a fallback route as a handler for handling missing routes. You can add a default route by adding a defaultRoute prop instead of the route prop.

function PageNotFound () {
  return <h1>Page Not Found</h1>
}

render(
  <Router>
    <Home route="/" />
    <About route="/about" />
    <PageNotFound defaultRoute />
  </Router>
  , document.getElementById("app")
);

Add A Route With Parameters

You can use path-to-regexp tokens in your route path to capture unknown segments as parameters. These will be made available as a params object in the props of the routed component.

function Greet ({ params }) {
  return (
    <div>Hello, {params.name}!</div>
  );
}

render(
  <Router>
    <Greet route="/greet/:name" />
  </Router>
  , document.getElementById("app")
);

Add a Function-Based Route

You may need more control over routing beyond a simple path string. For this, you can use a function which returns a boolean value for the route prop instead of a string.

var user = { role: "admin" };

function isAdmin (location) {
  return (user.role === "admin");
}

render(
  <Router>
    <Admin route={isAdmin} />
  </Router>
  , document.getElementById("app")
);

Nesting Routers & the partialRoute Prop

You can nest <Router> components with their own route props to create a hierarchy for routing. On top of that, you can use the partialRoute prop to match the first part of the window location to the route. It's important to note that nested routes will inherit the parent component's route as a prefix. So in the example below, the <Profile> component will be available at /account/profile.

render(
  <Router>
    <Home route="/" />
    <Router route="/account" partialRoute component={Account}>
      <Profile route="/profile" />
      <Settings route="/settings" />
    </Router>
  </Router>
  , document.getElementById("app")
);

Select-Style Routing

It's likely you'll run into a case where strict route paths collide with token paths. For example, if you had a /items/new route for creating new items and a /items/:id route for viewing or editing existing items. To resolve this, you can tell the <Router> to only match the first route-matchable child using the onlyShowFirst prop.

render(
  <Router onlyShowFirst>
    <CreateItem route="/items/new" />
    <ViewItem route="/items/:id" />
  </Router>
  , document.getElementById("app")
);

Route Redirects

Sometimes you may need to redirect users to other routes when certain conditions take place. This is where the withRedirect() higher-order component can be helpful. For example, let's say you have a group of routes that require the user to be logged in. We can use the withRedirect() function to create a new component that will redirect the user to the returned pathname from the given function.

import * as React from "react";
import { render } from "react-dom";
import { Router, withRedirect } from "stateside";

var state = { userLoggedIn: true };

function SecretLayout (props) {
  return (
    <div className="Secret">
      Logged In:
      {props.children}
    </div>
  );
}

const SecretSection = withRedirect(function (props) {
  return (!state.userLoggedIn ? "/login" : null);
})(SecretLayout);

render(
  <Router>
    <Login route="/login" />
    <Router component={SecretSection}>
      <SecretPage route="/" />
    </Router>
  </Router>
  , document.getElementById("app")
);

Add a Link

You can quickly link to routes using the provided <Link> component. The <Link> element takes a to prop which the URI you'd like to link to. If the to prop matches the current location, then you can have a class added to the element by passing the desired class name via the activeClassName prop.

Note: You can add your own style and className props like you normally would with an HTML element.

function Nav () {
  return (
    <nav>
      <Link to="/" className="Home">Home</Link>
      <Link to="/about" activeClassName="isActive">About</Link>
    </nav>
  );
}

Typescript

route Prop Issue

If intellisense or your compiler is complaining about adding Stateside specific props to your routed components (and any is not an option), then you can use the RouteProps interface exposed by this library.

import * as React from "react";
import {RouteProps} from "stateside";

export interface MyComponentProps extends RouteProps {
  someValue?: any;
}

export function MyComponent(props: MyComponentProps) {
  return (
    <div>
      Example Component
    </div>
  );
}