New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

redux-router5

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redux-router5

Router5 integration with redux

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.3K
decreased by-23.89%
Maintainers
1
Weekly downloads
 
Created
Source

redux-router5

Router5 integration with redux. If you develop with React, use this package with react-redux and react-router5. Using router5 with redux removes the need to include router5-listeners.

Example | Demo | Learn router5

Requirements

  • router5 >= 1.1.0
  • redux >= 3.0.0

How to use

  • Create and configure your router instance
  • Create and configure your store including router5Middleware and router5Reducer
  • Use routeNodeSelector on route nodes in your component tree
  • Use provided actions to perform routing
  • Since router5@1.1.0, canActivate and canDeactivate functions take store as their first argument

How it works

With redux

With React

import { ReactDOM } from 'react-dom';
import { RouterProvider } from 'react-router5';
import { Provider } from 'react-redux';
import React from 'react';
import App from './app';
import createRouter from './create-router';
import configureStore from './store';

const router = createRouter();
const store = configureStore(router);

router.start(() => {
    ReactDOM.render(
        (
            <Provider store={ store }>
                <RouterProvider router={ router }>
                    <App />
                </RouterProvider> 
            </Provider>
        ),
        document.getElementById('app')
    );
});

Note: RouterProvider comes from react-router5. It simply adds your router instance in your application context, which is required. Alternatively, you can use withContext()` from recompose.

router5Middleware

import createRouter from './create-router';
import { compose, createStore, applyMiddleware, combineReducers } from 'redux';
import { router5Middleware, router5Reducer } from 'redux-router5';

function configureStore(router, initialState = {}) {
    const createStoreWithMiddleware = applyMiddleware(router5Middleware(router))(createStore);

    const store = createStoreWithMiddleware(combineReducers({
        router: router5Reducer,
        /* your reducers */
    }), initialState);

    return store;
}

const router = createRouter();
const store = configureStore(router, { router: { route: state }});

router.start();

Under the hood, it simply adds a plugin to your router instance so your router dispatches actions on transition start, error, success and cancel (You can read more about router5 plugins here). It also relay navigateTo actions to the router.

router5Reducer

A simple reducer which is added by router5Middleware. Note: use router for your reducer key name, other names are not yet supported. router5Reducer will manage a piece of your state containing the following data attributes:

  • route
  • previousRoute
  • transitionRoute (the current transitioning route)
  • transitionError (the last error which occured)

route and previousRoute have a name, params and path properties.

Actions

Available actions

  • navigateTo(routeName, routeParams = {}, routeOptions = {})
  • cancelTransition()
  • clearErrors()
import { actions } from 'redux-router5';

routeNodeSelector

routeNodeSelector is a selector created with reselect. It is designed to be used on a route node and works with connect higher-order component from react-redux.

If your routes are nested, you'll have a few route nodes in your application. On each route change, only one route node needs to be re-rendered. That route node is the highest common node between your previous route and your current route. routeNodeSelector will only trigger a re-render when it needs to.

Then it is just a matter of returning the right component depending on the current route. Your virtual tree will react to route changes, all of that by simply leveraging the power of connect and reselect!

router5.helpers provides a set of functions to help making those decisions (useful if you have nested routes).

import { connect } from 'react-redux';
import { routeNodeSelector } from 'redux-router5';
import { Home, About, Contact, Admin, NotFound } from './components';
import { startsWithSegment } from 'router5.helpers';

function Root({ route }) {
    const { params, name } = route;

    const testRoute = startsWithSegment(name);

    if (testRoute('home')) {
        return <Home params={ params } />;
    } else if (testRoute('about')) {
        return <About params={ params } />;
    } else if (testRoute('contact')) {
        return <Contact params={ params } />;
    } else if (testRoute('admin')) {
        return <Admin params={ params } />;
    } else {
        return <NotFound />;
    }
}

export default connect(routeNodeSelector(''))(Root);

Keywords

FAQs

Package last updated on 07 Jan 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc