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-motor

v1.2.0

Published

A React router for navigation between arbitrary states. You create a motor with a custom state-to-path encoder/decoder and use it to navigate around your application.

Downloads

3

Readme

react-motor 🏍

A React router for navigation between arbitrary states. You create a motor with a custom state-to-path encoder/decoder and use it to navigate around your application.

Contents

Features

  • Synchronizes state with browser URL through the browser history
  • Custom URL encoding/decoding logic defined in a single location
  • Navigate to desired state with the navigate function
  • Features a declarative Link component (<a> with href)
  • Supports major browsers including IE11
  • Written in TypeScript (type definitions included)
  • Requires React 16.8 or higher with Hooks and Context API
  • Functional, lightweight and zero dependencies

Installation

  • NPM: npm install react-motor
  • Yarn: yarn add react-motor

Usage

  1. Call createMotor to build a motor with your stateToPath and pathToState functions.
  2. Wrap your application with the returned MotorProvider.
  3. Use the useMotor hook when you want to inspect state or navigate imperatively.
  4. Render the Link component when you want to navigate declaratively.

Example

This example shows how to use motor to encode javascript objects into base64 url.

  • State: { page: "post", id: 124 }
  • Resulting url: http://yourapp/eyJwYWdlIjoicG9zdCIsImlkIjoxMjR9

Start with creating a motor with a json and base64 encoder:

import { createMotor, RedirectMotor, b64encode, b64decode } from "react-motor";

// Define an arbitrary state shape
const defaultState = { page: "home" };

// Define custom path encoding/decoding (e.g. json+base64)
const options = {
  stateToPath: state => b64encode(JSON.stringify(state)),
  pathToState: path => {
    if (path === "") {
      // Redirect to home on root url
      throw new RedirectMotor(defaultState);
    }

    try {
      return JSON.parse(b64decode(path));
    } catch (err) {
      return { page: "error" };
    }
  }
};

// Create motor objects
const { MotorProvider, useMotor, Link } = createMotor(defaultState, options);

Wrap your app contents with the MotorProvider:

import React from "react";

function App() {
  return (
    <MotorProvider>
      <AppContent />
    </MotorProvider>
  );
}

Use useMotor hook or Link component anywhere:

function Tabs() {
  const { state, navigate } = useMotor();

  const className = active => (active ? "tab active" : "tab");

  return (
    <div className="tabs">
      <div
        onClick={() => navigate({ page: "home" })}
        className={className(state.page === "home")}
        style={{ cursor: "pointer" }}
      >
        Home
      </div>

      <Link to={{ page: "library" }}>
        <div className={className(state.page === "library")}>Library</div>
      </Link>
    </div>
  );
}

More Examples