• This repository has been archived on 15/Dec/2018
  • Stars
    star
    1,055
  • Rank 42,021 (Top 0.9 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tiny router for Redux that lets the URL do the talking.

Project Deprecated

As of December 10, 2018, this project is no longer under active maintenance. There are a few reasons we at Formidable made this decision:

  • React Router and its ecosystem have solved many of the problems this library was originally created for.
  • There are other alternatives that store React Router state in Redux, so you get the benefits of their view layer while keeping router state in your store. We recommend connected-react-router.
  • React Router v4's component APIs are more suited to presentational / connected components than prior versions. With that, it becomes more ergonomic to use those components even when routing with Redux.
  • We are hard at work at other new and exciting open source!

We appreciate all of the effort our maintainers and collaborators in the community have put into this project. We're proud to have made the world of Redux routing a little bit better with all of you. 🎉

redux-little-router

Build Status codecov npm

redux-little-router is a tiny router for Redux applications that lets the URL do the talking.

The router follows three basic principles:

  • The URL is just another member of the state tree.
  • URL changes are just plain actions.
  • Route matching should be simple and extendable.

While the core router does not depend on any view library, it provides flexible React bindings and components.

Why another router?

To understand why redux-little-router exists, check out our blog series, "Let the URL do the Talking":

Part 1 Part 2 Part 3

While React Router is a great, well-supported library, it hoards URL state within the view layer and makes certain Redux patterns difficult, if not impossible. This chart outlines a major issue in accessing URL state from outside of React Router.

react-router-redux is meant only to enable time-travel debugging in React Router and doesn't allow you to safely access URL state. redux-router, while allowing you to access URL state, is experimental, lags behind React Router releases, and recommends react-router-redux in its README.

redux-little-router makes URL state a first-class citizen of your Redux store and abstracts cross-browser navigation and routing into a pure Redux API.

Redux usage

To hook into Redux applications, redux-little-router uses a store enhancer that wraps the history module and adds current and previous router state to your store. The enhancer listens for location changes and dispatches rich actions containing the URL, parameters, and any custom data assigned to the route. redux-little-router also adds a middleware that intercepts navigation actions and calls their equivalent method in history.

Wiring up the boilerplate

The following is an example of a redux-little-router setup that works for browser-rendered applications. For a server rendering example, check out our advanced docs.

import { combineReducers, compose, createStore, applyMiddleware } from 'redux';
import { routerForBrowser } from 'redux-little-router';

import yourReducer from './your-app';

// Define your routes in a route-to-anything hash like below.
// The value of the route key can be any serializable data,
// including an empty object.

// This data gets attached to the `router.result` key of the state
// tree when its corresponding route is matched and dispatched.
// Useful for page titles and other route-specific data.

// Uses https://github.com/snd/url-pattern for URL matching
// and parameter extraction.
const routes = {
  '/messages': {
    title: 'Message'
  },
  '/messages/:user': {
    title: 'Message History'
  },
  // You can also define nested route objects!
  // Just make sure each route key starts with a slash.
  '/': {
    title: 'Home',
    '/bio': {
      title: 'Biographies',
      '/:name': {
        title: 'Biography for:'
      }
    }
  }
};

// Install the router into the store for a browser-only environment.
// routerForBrowser is a factory method that returns a store
// enhancer and a middleware.
const { reducer, middleware, enhancer } = routerForBrowser({
  // The configured routes. Required.
  routes,
  // The basename for all routes. Optional.
  basename: '/example'
});

const clientOnlyStore = createStore(
  combineReducers({ router: reducer, yourReducer }),
  initialState,
  compose(enhancer, applyMiddleware(middleware))
);

Often, you'll want to update state or trigger side effects after loading the initial URL. To maintain compatibility with other store enhancers (particularly ones that handle side effects, like redux-loop or redux-saga), we require this optional initial dispatch to happen in client code by doing the following:

import { initializeCurrentLocation } from 'redux-little-router';

// ...after creating your store
const initialLocation = store.getState().router;
if (initialLocation) {
  store.dispatch(initializeCurrentLocation(initialLocation));
}

Provided actions and state

redux-little-router provides the following action creators for navigation:

import { push, replace, go, goBack, goForward } from 'redux-little-router';

// `push` and `replace`
//
// Equivalent to pushState and replaceState in the History API.
// If you installed the router with a basename, `push` and `replace`
// know to automatically prepend paths with it. Both action creators
// accept string and object arguments.
push('/messages');

// Parsed query string stored in the `query` field of router state
push('/messages?filter=business');

// Provided query object stringified into the `search` field of router state
replace({
  pathname: '/messages',
  query: {
    filter: 'business'
  }
});

// Optional second argument accepts a `persistQuery` field. When true,
// reuse the query object from the previous location instead of replacing
// or emptying it.
push(
  {
    pathname: '/messages',
    query: {
      filter: 'business'
    }
  },
  {
    persistQuery: true
  }
);

// Navigates forward or backward a specified number of locations
go(3);
go(-6);

// Equivalent to the browser back button
goBack();

// Equivalent to the browser forward button
goForward();

// Creates a function that blocks navigation with window.confirm when returning a string.
// You can customize how the prompt works by passing a `historyOptions` option with a
// `getUserConfirmation` function to `routerForBrowser`, `routerForExpress`, etc.
// See https://www.npmjs.com/package/history#blocking-transitions
block((location, action) => {
  if (location.pathname === '/messages') {
    return 'Are you sure you want to leave the messages view?';
  }
});

// Removes the previous `block()`.
unblock();

Note: if you used the vanilla action types prior to v13, you'll need to migrate to using the public action creators.

These actions will execute once dispatched. For example, here's how to redirect using a thunk:

import { push } from 'redux-little-router';

export const redirect = href => dispatch => {
  dispatch(push(href));
};

On location changes, the store enhancer dispatches a LOCATION_CHANGED action that contains at least the following properties:

// For a URL matching /messages/:user
{
  pathname: '/messages/a-user-has-no-name',
  route: '/messages/:user',
  params: {
    user: 'a-user-has-no-name'
  },
  query: { // if your `history` instance uses `useQueries`
    some: 'thing'
  },
  search: '?some=thing',
  result: {
    arbitrary: 'data that you defined in your routes object!'
    parent: { // for nested routes only
      // contains the result of the parent route,
      // which contains each other parent route's
      // result recursively
    }
  }
}

Your custom middleware can intercept this action to dispatch new actions in response to URL changes.

The reducer consumes this action and adds the following to the root of the state tree on the router property:

{
  pathname: '/messages/a-user-has-no-name',
  route: '/messages/:user',
  params: {
    user: 'a-user-has-no-name'
  },
  query: {
    some: 'thing'
  },
  search: '?some=thing',
  result: {
    arbitrary: 'data that you defined in your routes object!',
    parent: { /* the parent route's result */ },
  },
  previous: {
    pathname: '/messages',
    route: '/messages',
    params: {},
    query: {},
    result: {
      more: 'arbitrary data that you defined in your routes object!'
      parent: { /* the parent route's result */ }
    }
  }
}

Your custom reducers or selectors can derive a large portion of your app's state from the URLs in the router property.

React bindings and usage

redux-little-router provides the following to make React integration easier:

  • A <Fragment> component that conditionally renders children based on current route and/or location conditions.
  • A <Link> component that sends navigation actions to the middleware when tapped or clicked. <Link> respects default modifier key and right-click behavior. A sibling component, <PersistentQueryLink>, persists the existing query string on navigation.

Instances of each component automatically connect() to the router state with react-redux.

You can inspect the router state in any child component by using connect():

const mapStateToProps = state => ({ router: state.router });
export default connect(mapStateToProps)(YourComponent);

<Fragment>

Think of <Fragment> as the midpoint of a "flexibility continuum" that starts with raw switch statements and ends with React Router v3's <Route> component. Fragments can live anywhere within the React tree, making split-pane or nested UIs easy to work with.

The simplest fragment is one that displays when a route is active:

<Fragment forRoute="/home/messages/:team">
  <p>This is the team messages page!</p>
</Fragment>

You can also match a fragment against anything in the current location object:

<Fragment withConditions={location => location.query.superuser}>
  <p>Superusers see this on all routes!</p>
</Fragment>

You can use withConditions in conjunction with forRoute to set strict conditions for when a <Fragment> should display.

To show a Fragment when no other Fragments match a route, use <Fragment forNoMatch />.

<Fragment> lets you nest fragments to match your UI hierarchy to your route hierarchy, much like the <Route> component does in react-router@v3. Given a URL of /about/bio/dat-boi, and the following elements:

<Fragment forRoute="/about">
  <div>
    <h1>About</h1>
    <Fragment forRoute="/bio">
      <div>
        <h2>Bios</h2>
        <Fragment forRoute="/dat-boi">
          <div>
            <h3>Dat Boi</h3>
            <p>Something something whaddup</p>
          </div>
        </Fragment>
      </div>
    </Fragment>
  </div>
</Fragment>

...React will render:

<div>
  <h1>About</h1>
    <div>
      <h2>Bios</h2>
        <div>
          <h3>Dat Boi</h3>
          <p>Something something whaddup<p>
        </div>
    </div>
</div>

<Fragment> makes basic component-per-page navigation easy:

<Fragment forRoute="/">
  <div>
    <Fragment forRoute="/">
      <Home />
    </Fragment>
    <Fragment forRoute="/about">
      <About />
    </Fragment>
    <Fragment forRoute="/messages">
      <Messages />
    </Fragment>
    <Fragment forRoute="/feed">
      <Feed />
    </Fragment>
  </div>
</Fragment>

<Link>

Using the <Link> component is simple:

<Link className="anything" href="/yo">
  Share Order
</Link>

Alternatively, you can pass in a location object to href. This is useful for passing query objects:

<Link
  className="anything"
  href={{
    pathname: '/home/messages/a-team?test=ing',
    query: {
      test: 'ing'
    }
  }}
>
  Share Order
</Link>

To change how <Link> renders when its href matches the current location (i.e. the link is "active"), use activeProps. For example, you can add className to activeProps to use a different CSS class when the link is active:

<Link
  href="/wat"
  className="normal-link"
  activeProps={{ className: 'active-link' }}
>
  Wat
</Link>

<Link> takes an optional valueless prop, replaceState, that changes the link navigation behavior from pushState to replaceState in the History API.

Use with immutable

redux-little-router supports the use of immutable.js in tandem with an immutable-aware combineReducers function like provided by redux-immutable. To use it, you will need to import the immutable version of the router or component you want to use. For instance,

import { immutableRouterForBrowser, ImmutableLink } from 'redux-little-router/es/immutable';
import { combineReducers } from 'redux-immutable';

const { reducer, enhancer, middleware } = immutableRouterForBrowser({ routes });

const store = createStore(
  combineReducers({ router: reducer, ... }),
  ...
);

Depending on your environment, you might need to modify the import statement further. In that case, here are some tips:

// works: ESM (preferred for webpack2+)
import { immutableRouterForBrowser } from 'redux-little-router/es/immutable';

// works: CJS (preferred for webpack1 or Node.js)
import { immutableRouterForBrowser } from 'redux-little-router/lib/immutable';

// DOESN'T WORK
import { immutableRouterForBrowser } from 'redux-little-router/immutable';

Environment

redux-little-router requires an ES5 compatible environment (no IE8).

Stability

We consider redux-little-router to be stable. Any API changes will be incremental.

Versioning

redux-little-router follows strict semver. Don't be alarmed by the high version number! Major version bumps represent any breaking change, no matter how small, and do not represent a major shift in direction. We strive to make breaking changes small and compartmentalized.

Contributing

We welcome community contributions! We have standardized our dev experience on yarn so make sure to have that installed.

$ git clone [email protected]:FormidableLabs/redux-little-router.git
$ cd redux-little-router
$ yarn install

After any changes and before a PR, make sure to pass our build and quality checks:

$ yarn run build
$ yarn run check

When ready for release, we use an npm version workflow:

$ npm version <major|minor|patch|ACTUAL_VERSION_NUMBER>
$ npm publish
$ git push && git push --tags

After publishing, consider drafting some release notes to let the world know about all the great new features!

Community

More Repositories

1

webpack-dashboard

A CLI dashboard for webpack dev server
JavaScript
13,886
star
2

victory

A collection of composable React components for building interactive data visualizations
JavaScript
10,570
star
3

spectacle

A React-based library for creating sleek presentations using JSX syntax that gives you the ability to live demo your code.
TypeScript
9,622
star
4

urql

The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
TypeScript
7,504
star
5

radium

A toolchain for React component styling.
JavaScript
7,419
star
6

react-game-kit

Component library for making games with React & React Native
JavaScript
4,588
star
7

react-live

A flexible playground for live editing React components
TypeScript
3,990
star
8

nodejs-dashboard

Telemetry dashboard for node.js apps from the terminal!
JavaScript
3,921
star
9

react-animations

🎊 A collection of animations for inline style libraries
JavaScript
3,063
star
10

nuka-carousel

Small, fast, and accessibility-first React carousel library with an easily customizable UI and behavior to fit your brand and site.
TypeScript
2,980
star
11

react-music

Make beats with React!
JavaScript
2,721
star
12

electron-webpack-dashboard

Electron Desktop GUI for Webpack Dashboard
JavaScript
2,717
star
13

victory-native

victory components for react native
JavaScript
2,007
star
14

react-swipeable

React swipe event handler hook
TypeScript
1,945
star
15

react-native-app-auth

React native bridge for AppAuth - an SDK for communicating with OAuth2 providers
Java
1,869
star
16

prism-react-renderer

🖌️ Renders highlighted Prism output to React (+ theming & vendored Prism)
TypeScript
1,764
star
17

freactal

Clean and robust state management for React and React-like libs.
JavaScript
1,664
star
18

react-fast-compare

fastest deep equal comparison for React
JavaScript
1,516
star
19

rapscallion

Asynchronous React VirtualDOM renderer for SSR.
JavaScript
1,395
star
20

component-playground

A component for rendering React components with editable source and live preview
JavaScript
1,187
star
21

react-progressive-image

React component for progressive image loading
JavaScript
744
star
22

react-native-owl

Visual regression testing library for React Native that enables developers to introduce visual regression tests to their apps.
TypeScript
613
star
23

renature

A physics-based animation library for React focused on modeling natural world forces.
TypeScript
604
star
24

inspectpack

An inspection tool for Webpack frontend JavaScript bundles.
TypeScript
589
star
25

react-ssr-prepass

A custom partial React SSR renderer for prefetching and suspense
JavaScript
583
star
26

spectacle-boilerplate

[DEPRECATED] Boilerplate project for getting started with Spectacle Core
581
star
27

use-editable

A small React hook to turn elements into fully renderable & editable content surfaces, like code editors, using contenteditable (and magic)
TypeScript
439
star
28

appr

Open React Native PR Builds instantly on device
JavaScript
381
star
29

image-palette

Generate a WCAG compliant color theme from any image
JavaScript
356
star
30

webpack-stats-plugin

Webpack stats plugin for build information, file manifests, etc.
JavaScript
351
star
31

formidable-react-native-app-boilerplate

React Native / Redux / Babel boilerplate.
JavaScript
340
star
32

react-native-zephyr

TailwindCSS-inspired styling library for React Native.
TypeScript
339
star
33

builder

An npm-based task runner
JavaScript
320
star
34

victory-cli

A tool for generating charts on the command line.
JavaScript
311
star
35

runpkg

the online javascript package explorer
JavaScript
307
star
36

seattlejsconf-app

ReasonML React Native App for SeattleJS Conf
OCaml
303
star
37

victory-native-xl

A charting library for React Native with a focus on performance and customization.
TypeScript
299
star
38

victory-chart

Chart Component for Victory
JavaScript
290
star
39

serverless-jetpack

A faster JavaScript packager for Serverless applications.
JavaScript
273
star
40

react-flux-concepts

Step by step building the recipe app in react & flux.
HTML
269
star
41

eslint-plugin-react-native-a11y

React Native specific accessibility linting rules.
JavaScript
265
star
42

react-shuffle

Animated shuffling of child components on change
JavaScript
251
star
43

babel-plugin-transform-define

Compile time code replacement for babel similar to Webpack's DefinePlugin
JavaScript
247
star
44

groqd

A schema-unaware, runtime and type-safe query builder for GROQ.
TypeScript
210
star
45

react-native-ama

Accessibility as a First-Class Citizen with React Native AMA
TypeScript
209
star
46

urql-devtools

A tool for monitoring and debugging urql during development
TypeScript
204
star
47

react-native-responsive-styles

React Native styles that respond to orientation change
JavaScript
170
star
48

es6-interactive-guide

An interactive guide to ES6
JavaScript
164
star
49

terraform-aws-serverless

Infrastructure support for Serverless framework apps, done the right way
HCL
143
star
50

whackage

Multi-repo development tooling for React Native
JavaScript
132
star
51

formidable-playbook

The Formidable development playbook.
132
star
52

clips

Create short shareable screen recordings – all using web APIs
Svelte
126
star
53

radium-grid

A powerful, no-fuss grid system component for React
JavaScript
123
star
54

github-2049

JavaScript
123
star
55

pino-lambda

Send pino logs to cloudwatch with aws-lambda
TypeScript
113
star
56

ecology

Documentation generator for collections of react components.
JavaScript
107
star
57

formidable-react-starter

React starter application
JavaScript
95
star
58

publish-diff

Preview npm publish changes.
JavaScript
91
star
59

urql-exchange-graphcache

A normalized and configurable cache exchange for urql
89
star
60

yesno

Simple HTTP testing for NodeJS
TypeScript
89
star
61

measure-text

An efficient text measurement function for the browser.
JavaScript
87
star
62

spectacle-boilerplate-mdx

[DEPRECATED] Boilerplate that facilitates using MDX with Spectacle
81
star
63

css-to-radium

Radium migration CLI, converts CSS to Radium-compatible JS objects.
JavaScript
79
star
64

envy

Node.js Telemetry & Network Viewer
TypeScript
76
star
65

victory-core

Shared libraries and components for Victory
JavaScript
72
star
66

aws-lambda-serverless-reference

A reference application for AWS + serverless framework.
HCL
70
star
67

jest-next-dynamic

Resolve Next.js dynamic import components in Jest tests
JavaScript
69
star
68

formidable-charts

Ready-made composed Victory components
JavaScript
67
star
69

victory-uiexplorer-native

A React Native app for iOS and Android that showcases Victory Native components
JavaScript
65
star
70

pull-report

Create reports for open GitHub pull requests / issues for organizations and users.
JavaScript
64
star
71

react-context-composer

[DEPRECATED] Clean composition of React's new Context API
JavaScript
60
star
72

victory-pie

D3 pie & donut chart component for React
JavaScript
60
star
73

recipes-flux

Recipes (Flux example)
JavaScript
59
star
74

next-urql

Convenience utilities for using urql with NextJS.
TypeScript
56
star
75

lank

Link and control a bunch of repositories.
JavaScript
49
star
76

full-stack-testing

Full. Stack. Testing. (w/ JavaScript)
JavaScript
47
star
77

converter-react

Sample React + Flux app w/ server-side rendering / data bootstrap and more!
JavaScript
44
star
78

urql-exchange-suspense

An exchange for client-side React Suspense support in urql
43
star
79

victory-animation

DEPRECATED-- Use victory-core
JavaScript
42
star
80

react-native-animation-workshop

React Native Animations & Interactions Workshop
JavaScript
41
star
81

notes-react-exoskeleton

Notes using React + Exoskeleton
JavaScript
39
star
82

graphql-typescript-blog

TypeScript
39
star
83

victory-chart-native

JavaScript
37
star
84

react-europe-demos

React Europe 2018 Keynote Demos
JavaScript
37
star
85

react-synth

React synth demo code for http://reactamsterdam.surge.sh
JavaScript
37
star
86

urql-devtools-exchange

The exchange for usage with Urql Devtools
TypeScript
35
star
87

victory-native-demo

Demo victory-native
JavaScript
35
star
88

victory-tutorial

A tutorial for Victory used with the Getting Started guide in Victory Docs.
JavaScript
34
star
89

multibot

A friendly multi-repository robot.
JavaScript
31
star
90

gql-workshop-app

Real World GraphQL
JavaScript
31
star
91

trygql

Purpose-built Demo APIs for GraphQL; never write a schema for your client-side GraphQL demo apps twice.
JavaScript
31
star
92

victory-docs

Documentation for Victory: A collection of composable React components for building interactive data visualizations
JavaScript
29
star
93

react-europe-workshop

JavaScript
28
star
94

rowdy

A small, rambunctious WD.js / WebdriverIO configuration wrapper.
JavaScript
28
star
95

eslint-config-formidable

A set of default eslint configurations from Formidable
JavaScript
27
star
96

nextjs-sanity-fe

NextJS Demo site with Sanity CMS
TypeScript
27
star
97

spectacle-cli

CLI for the Spectacle Presentation Framework
JavaScript
27
star
98

trace-pkg

A dependency tracing packager for Node.js source files.
26
star
99

radium-constraints

Constraint-based layout system for React components.
JavaScript
26
star
100

mock-raf

A simple mock for requestAnimationFrame testing with fake timers
JavaScript
25
star