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

switcheroo

v4.0.0-alpha.1

Published

React component that switches between components based on route

Downloads

2,982

Readme

npm version Build Status Dependency Status

switcheroo

switcheroo allows you to specify a React container component that renders a single child component based on a property of window.location (hash, pathname, etc.), using window.location.hash by default.

The Switcher container component provided by switcheroo can accept any React elements, as long as they have a path property. switcheroo is very configurable, and you can read about the properties the Switcher and children elements (that we will refer to as "Switches") take in the docs. A higher order component to help improve Switcher performance in larger applications is also provided, SwitcherProvider.

Installation

npm

npm install --save switcheroo

cdn

While the npm package is recommended for production usage, if you just want to drop a <script> tag on your page you can also use the UMD/global build hosted on unpkg.

<script src="https://unpkg.com/switcheroo"></script>

Try it out

You can try out switcheroo now on jsbin. Or see it in action powering Custom Ink's design lab.

Features

  • Router agnostic. You can use any router, even react-router, in conjunction with switcheroo
  • Any React component can be used as a "Switch" without any modification, other than defining a path property on it.
  • Supports hashChange and pushState
  • Provides callbacks including when the path changes
  • Supports dynamic path segments and passes dynamic segment data to "Switch" component as props.
  • Passes query parameters to "Switch" component as props.
  • Supports React animations via wrapper prop
  • Highly configurable via props
  • Lightweight ~2k gzipped

Example Usage

import Switcher from 'switcheroo';

<Switcher>
  <HomeComponent path="/" />
  <AboutComponent path="/about" someAboutComponentProp="thisOne" />
  <StoreComponent path="/store">
    <ItemComponent />
  </StoreComponent>
  <UserComponent path="/user/:id" />
</Switcher>

Transitions and Animations

You can use the wrapper property with transition group elements like React's CSSTransitionGroup addon or Twitter Fabric's velocity-react to make Switch elements animate as they enter and leave.

See the animation example to see animations in action.

Rationale

The purpose of switcheroo is to enable switching what React component is rendered based on the configured part of the URL without forcing any routing opinions on you, you can use whatever router you wish. This helps keep switcheroo small and flexible. These design decisions also enable "decentralized routing" and more flexible and dynamic layouts.

  • Decentralized routing: You can build out shareable React components using switcheroo and not worry about these components having knowledge of the router. Only the top level app that the components are being imported into needs to know about the router.
  • Flexible and dynamic layouts: Most routing solutions have the notion of layouts, where each route has an explicit layout that is rendered on that route. This means if the components that make up those routes appear in multiple layouts, you need to define a top level layout for each combination that you desire, which can be repetitious. switcheroo's decentralized nature allows each "Switch" to specify all of the routes for which it should render, which means each layout can be dynamic. This prevents the case of having to create an entirely new layout for one small difference between an existing layout and will generally lead to less repetition/duplication in layouts. In addition to this, Switchers can be nested infinitely, which allows for greater flexibility while still being expressive.

If you are looking for a more robust and opinionated routing solution, I highly recommend taking a look at react-router. This project actually spawned from an attempt to do something similar with react-router. Update: As of React Router v4, this is easily doable with v4's awesome component based API. The decision to use switcheroo or React Router now depends on if you need a full routing solution with history management etc. or just the ability to manage component presentation based on routes (which could be in conjunction with another routing solution). That said you could use both together without any issue, but if you already bring in React Router you can just use their built in Switch component.