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 🙏

© 2026 – Pkg Stats / Ryan Hefner

redux-callback-router

v0.1.1

Published

State-based routing using Redux

Downloads

32

Readme

Redux Callback Router

This is the Redux flavor of Callback Router, which allows you to keep the URL synchronized with your Redux state In addition to generic callbacks, you can map routes to action creators so that navigation will dispatch an action.

Install

yarn:

yarn add redux-callback-router

npm:

npm install redux-callback-router

Usage

import {
  ROUTE_CHANGE_INITIALIZE,
  createCallbackRouterReducer,
  registerRoutes,
} from 'redux-callback-router';

import yourReducer from './reducer';

// Map the state to paths
function mapStateToPath(state, action, prevState) {
  if (state.id) {
    if (action.type === 'CHANGE_USER') {
      // Return a string for the pushState destination
      return `/users/${id}`;
    } else {
      // Return [string, options] to customize navigation
      return [`/users/${id}`, { replaceState: true }];
    }
  }
  // Otherwise, leave path unchanged
  return null;
}

// Wrap the router to update the path on state changes
const reducer = createCallbackRouterReducer(yourReducer, mapStateToPath);

// Use the wrapped reducer to create your Redux store
const store = createStore(reducer);

// Register the routes
const {
  unregisterRoutes,
  evaluate,
} = registerRoutes({
  '/users': {
    type: 'SHOW_USERS',
  },
  '/users/:id': {
    action: (params) => ({
      type: 'SHOW_USER',
      payload: {
        userId: params.id,
      },
    }),
    last: true, // prevent '/users' route from firing
  },
}, store.dispatch, store.getState);

// Evaluate the current URI
evaluate(
  document.location.pathname,
  window.history.state,
  ROUTE_CHANGE_INITIALIZE,
);

// Unregister the routes
unregisterRoutes();

API


registerRoutes

Registers paths to their callbacks.

Returns functions to unregister the routes and evaluate a given path.

const {
  unregisterRoutes,
  evaluate,
} = registerRoutes(routes, callback);

Returns { unregisterRoutes, evaluate }.

routes (Object)

Map of paths to route definitions, actions creators, or actions. The path may contain named :params or (.*) wildcards.

Route definitions have the following properties:

action (Object)

A Redux action that is dispatched when the route is matched.

action(params, type, pathname, state, path, getState)

Function that generates an action to be dispatched when the route is matched.

callback(params, type, dispatch, getState, pathname, state, path)

Function invoked when the route is matched.

This is an alternative to action for cases where a route needs to dispatch multiple actions or have some other special behavior.

exact (boolean, defaults to false)

When set, this will not honor a partial match. For example a URI /user/123 would not match the route /user.

last (boolean, defaults to true)

When set, a match on this route will prevent subsequent lower-priority routes from being evaluated.

navigate (boolean, defaults to false)

When set, this route will be evaluated for imperative navigation (pushState and replaceState) whether or not navigation is forced.

strict (boolean, defaults to false)

When set, the trailing slash must be present or omitted exactly as the route is defined. For example, a URI /users would not match the route /users/, or vise-versa.

callback(result) (Function, optional)

Common callback invoked at the end of any matching route's callback.


evaluate

Determines which routes match the pathname and invokes their callbacks.

Returns the value returned from the callback of the highest-priority (first) matching route.

const result = evaluate(
  pathname,
  state,
  type,
  routes,
);
pathname (string, defaults to document.location.pathname)

URI to evaluate

state (Object, defaults to window.history.state)

Additional state data associated with the current history item.

type (string)

Navigation type used to differentiate between imperative navigation and browser navigation.

  • ROUTE_CHANGE_INITIALIZE: Router was first registered
  • ROUTE_CHANGE_PUSH_STATE: Imperative navigation (skips callback)
  • ROUTE_CHANGE_REPLACE_STATE: Imperative navigation (skips callback)
  • ROUTE_CHANGE_POP_STATE: Browser navigation (invokes callback)
  • ROUTE_CHANGE_FORCE_PUSH_STATE: Imperative navigation (invokes callback)
  • ROUTE_CHANGE_FORCE_REPLACE_STATE: Imperative navigation (invokes callback)
  • ROUTE_CHANGE_UNKNOWN: Unknown navigation type
routes (Object, defaults to all registered routes)

Map of paths to route definitions or callbacks.


navigate

Performs imperative navigation.

By default, this only modifies history using pushState or replaceState. Routes without navigate enabled are not evaluated unless the force option is enabled!

Returns the result of the callback if a route was evaluated.

const result = navigate(path, {
  force,
  replaceState,
  state,
  title,
});
path (string)

Destination pathname.

options.force (boolean, defaults to false)

When set, this will evaluate non-navigate routes.

options.replaceState (boolean, defaults to false)

When set, this replaces the current history state instead of pushing an additional state.

options.state (Object, defaults to {})

Additional data associated with the state.

options.title (string, defaults to document.title)

Title of the history state.


createCallbackRouterReducer

Creates a wrapped reducer that can update the history based on the state.

const wrappedReducer = createCallbackRouterReducer(
  reducer,
  mapStateToPath,
  navigateInitialState, // default: false
);
reducer (Function)

Reducer to connect to the Callback Router.

mapStateToPath(state, action, prevState)

Function that determines the URL from the state (and/or action).

The return value is used to determine whether and how navigate is invoked. Return a string to push a new history state. Return an array with a string and an object to specify the navigation options.

navigateInitialState (boolean, defaults to false)

The initial reducer call doesn't represent a state change, so navigation doesn't typically make sense. Setting this will ignore that and attempt to navigate after the initial reducer invocation.

License

ISC © Keith McKnight