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

react-hash-route

v1.1.0

Published

a much simpler way to support routing in React applications

Downloads

46

Readme

react-hash-route

This small library provides support for hash-based routing in the client side of web applications. It is primarily intended for use in React applications, but may also be useful with other frameworks. This is much simpler than using react-router and does not use JSX-based syntax for route configuration.

Benefits

  • This library is much simpler to learn and use than react-router, yet it does everything I need for routing.
  • Routing is a kind of configuration that is different from UI markup. Using JSX for this feels wrong, so this library doesn't do that.
  • This library makes it very easy to change the route inside a component method. It just requires a call to the route function , passing it a hash name.
  • Nothing extra is required to support remembering the route if the user refreshes the browser. Getting this to work with react-router seems tedious.

Setup

In the topmost source file, likely named index.js, add the following which assumes the topmost component is App:

import {routeSetup} from 'react-hash-route';

function render() {
  ReactDOM.render(<App />, document.getElementById('root'));
}

routeSetup(render);

Suppose your app has the following requirements:

  • There are pages to login, edit user profile, and place an order.
  • A common "header" should be rendered at the top of every page.
  • If an unsupported URL hash is used, the app should route to the login page.

In the source file that defines the App component add something like the following:

import {getHash} from 'react-hash-route';
import Header from './header';
import Login from './login';
import Order from './order';
import Profile from './profile';

const componentMap = {
  login: <Login />,
  profile: <Profile />,
  order: <Order />
};

const App = () => (
  <div>
    <Header />
    {componentMap[getHash() || 'login']}
  </div>
);

export default App;

Suppose the header component contains links for navigating to the login, order, and profile pages. Add something like the following in header.js.

import React, {Component} from 'react';
import {route} from 'react-hash-route';

class Header extends Component {

  onLogin = () => route('login');
  onOrder = () => route('order');
  onProfile = () => route('profile');

  render() {
    return (
      <div>
        <a key="login" onClick={this.onLogin}>Login</a>
        <a key="order" onClick={this.onOrder}>Order</a>
        <a key="profile" onClick={this.onProfile}>Profile</a>
      </div>
    );
  }
}

Suppose on the order page we want to verify that the user is logged in and reroute to the login page if they have not. Add something like the following in the render method of the Order component.

if (!authenticated) return route('login');

There are several approaches that can be used to make data available to component after a route change. Probably the best is to put data in Redux state and have other components can access it from there. Another approach is to use what this library refers to as "hash parameters". These are just values are appended to the route name and begin with a slash. For example, route('order/discount/20'). Inside the Order component, these "hash parameters" can be retrieved with the following:

import {getHashParameters} from 'react-hash-route';

// Inside some method ...
const [adjustment, amount] = getHashParameters();

That's everything to you need to know to use react-hash-route. Code simply!

If you like this, also check out https://www.npmjs.com/package/redux-easy.