Gathering detailed insights and metrics for redux-unity-router
Gathering detailed insights and metrics for redux-unity-router
Gathering detailed insights and metrics for redux-unity-router
Gathering detailed insights and metrics for redux-unity-router
Router, that syncs history with store and provides React bindings
npm install redux-unity-router
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
13 Stars
164 Commits
6 Forks
4 Watchers
323 Branches
3 Contributors
Updated on Apr 26, 2020
Latest Version
1.6.1
Package Id
redux-unity-router@1.6.1
Unpacked Size
110.44 kB
Size
25.97 kB
File Count
46
NPM Version
6.7.0
Node Version
11.10.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
6
25
Simple routing for your redux application.
The main purpose of this router is to mirror your browser history to the redux store and help you easily declare routes.
We also provide React bindings!
Install redux-unity-router
package from npm:
1npm i --save redux-unity-router
Before proceeding to the next step, we suggest you create a file containing your routes:
1/* routes.js */ 2 3export default { 4 id: 'Main', 5 pattern: '/application/', 6 data: { 7 pageTitle: 'My simple application' 8 }, 9 routes: [ 10 { 11 id: 'News', 12 pattern: '/news/', 13 data: { 14 pageTitle: 'My news' 15 }, 16 routes: [ 17 { 18 id: 'Item', 19 pattern: ':id' 20 } 21 ] 22 }, 23 { 24 id: 'Contacts', 25 pattern: '/contacts/' 26 } 27 ] 28};
You can learn more about setting up routes
and their structure in the API section.
Then require those routes
and set up your store
like this:
1/* store.js */ 2 3import { createStore, applyMiddleware, compose, combineReducers } from 'redux'; 4 5// Previously defined routes 6import routes from './routes.js'; 7 8import { createRouter, History } from 'redux-unity-router'; 9 10// Create history 11const history = History.createBrowserHistory(); 12 13// Create router instance 14const router = createRouter({ history, routes }); 15 16// Add router middleware to your list of middlewares 17const middleware = [ router.middleware ]; 18 19// Enhance your store by using router's enhancer 20const toEnhance = [ 21 router.enhancer, 22 applyMiddleware(...middleware) 23]; 24 25// Put it all together 26const enhancer = compose(...toEnhance); 27const reducers = combineReducers({ 28 router: router.reducer 29}); 30 31const initialState = {} 32 33const store = createStore(reducers, initialState, enhancer); 34 35export default store;
Now you've got yourself a simple routing system!
After navigating to /news/1?edit=true#title
you can expect your store's state to contain 'router'
entry similar to:
1{ 2 "pathname": "/news", 3 "search": "?edit=true", 4 "hash": "#title", 5 "key": "gmj9fs", 6 "query": {"edit": "true"}, 7 "state": {}, 8 "path": "/news/1?edit=true", 9 "route": { 10 "pattern": {"path": "/news/:id"}, 11 "id": "Item", 12 "idPath": "News:Item", 13 "params": {"id": "1"}, 14 "data": {"pageTitle": "My news"} 15 } 16}
You can manage your layout by using <RouterProvider>
, <Fragment>
and <Link>
React-components.
They should help keep your application simple and maintainable.
createRouter({ history, routes, immutable, slice })
1import { createRouter } from 'redux-unity-router'
Router factory, that returns an instance of the router object, containing: middleware, enhancer and reducer.
history
{Object}History object created by abstraction over browser's History API.
routes
{Array}An array of routes. If any of the routes can be matched to the same pattern, the route that has been declared first in routes
array will take precedence.
immutable
{Boolean} optionalDefault: false
If you use immutable store, set this to true
.
slice
{String} optionalDefault: 'router'
Store's key, that will contain router
's data.
route
{Object}An object containing route's definition.
pattern
{Object|String}Redux-Unity-Router uses path-to-regexp for route-matching. There's also a handy tool to test your patterns.
Although you can declare patterns in the form of string
s, that becomes problematic, when you want to match a route with query parameters, that may appear in arbitrary order. For this situation you may want to declare a pattern in a form of a plain object
:
path
{String} optionalSame as you'd declare a pattern in a form of a string.
query
{Object} optionalPlain query object.
id
{String} optional (but recommended)Default: equals pattern
if typeof pattern === 'string'
or pattern.path
if typeof pattern === 'object'
Unique Id of the route.
It is recommended that you define route's id, so you can easily navigate to it with <Link>
component.
data
{Object}Any arbitrary data you want reflected in the redux store, when the route is matched.
routes
{Array} optionalAny sub-routes a route may have. All the patterns and data of these sub-routes will be merged with their parent's . Sub-routes always take precedence over their parents in the matching process.
1const routes = [ 2 { 3 id: 'User', 4 pattern: '/user/:id', 5 data: { 6 pageTitle: 'User Profile' 7 } 8 routes: [ 9 { 10 id: 'UserEdit', 11 data: { 12 pageTitle: 'Edit User Profile' 13 }, 14 pattern: { 15 query: { 16 edit: 'true' 17 } 18 } 19 }, 20 { 21 id: 'UserLogout', 22 data: { 23 message: 'goodbye' 24 }, 25 26 pattern: { 27 path: 'logout' 28 } 29 } 30 ] 31 } 32 ] 33 34// This will produce 3 patterns: 35// { path: '/user/:id', query: { edit: 'true' }, data: { pageTitle: 'User Profile' } } 36// { path: '/user/:id/logout', data: { pageTitle: 'Edit User Profile' } } 37// { path: '/user/:id', data: { pageTitle: 'User Profile', message: ''goodbye' } }
1import { actions } from 'redux-unity-router'
or
1import actions from 'redux-unity-router/actions'
Actually, these are action-creators (functions, that produce plain action objects). You can use them if you want to programmatically navigate to any part of your application. Most of them correspond to standard methods of browser's History API (except for goToRoute
and locationChange
).
push(payload)
Navigate to new url/path.
payload
{String|Object}payload
of type string
will be interpreted as path or url.payload
of type object
should contain one the following properties:pathname
{String} optionale.g. '/news'
search
{String} optionale.g. '?edit=true'
hash
{String} optionale.g. '#title'
silent
{Boolean} optionalDefault: false
This extra option allows you to change current location url without propagating changes to the Redux store.
replace(payload)
Navigate to new url/path, replacing current history entry.
payload
{String/Object}Same as for push
action.
go(payload)
Go back or forward in history stack.
payload
{Integer}e.g. -1
goBack()
Equivalent to go(-1)
.
goForward()
Equivalent to go(1)
.
goToRoute(payload)
Navigate to the predefined route.
payload
{Object}id
{String}Valid route ID.
params
{Object} optionalIf our route contains parameters, you should provide them here.
query
{Object} optionalPlain query object.
hash
{String} optionalHash for the resulting url.
If you've defined a route like this:
1{ 2 id: 'Preferences', 3 pattern: '/prefs/:action' 4}
and you want to navigate to /prefs/edit?edit=true#title
, you should dispatch an action like this:
1store.dispatch(actions.goToRoute({ 2 id: 'Preferences', 3 params: { action: 'edit' }, 4 query: { edit: true }, 5 hash: 'title' 6}));
locationChange(payload)
You most likely will never use this one, as it is used by Redux-Unity-Router internally to produce an entirely new router state.
Check your store for this one!
1import { actionTypes } from 'redux-unity-router'
or
1import actionTypes from 'redux-unity-router/actionTypes'
Internally Redux-Unity-Router dispatches actions with following action-types
@@REDUX_UNITY_ROUTER/**LOCATION_CHANGED**
@@REDUX_UNITY_ROUTER/**PUSH**
@@REDUX_UNITY_ROUTER/**REPLACE**
@@REDUX_UNITY_ROUTER/**GO**
@@REDUX_UNITY_ROUTER/**GO_BACK**
@@REDUX_UNITY_ROUTER/**GO_FORWARD**
@@REDUX_UNITY_ROUTER/**GO_TO_ROUTE**
Keep in mind, that if you want to handle actions with these action-types in your reducers, all actions except for @@REDUX_UNITY_ROUTER/LOCATION_CHANGED will be swallowed by Redux-Unity-Router's middleware.
Although you can easily manage application's layout based on the router's redux store data, we do provide some handy react components for you to use:
<RouterProvider>
routes
{Array}An array of routes. We advice you use the same array for both createRouter
and <RouterProvider>
.
immutable
{Boolean}Default: false
If you use immutable store, set this to true
.
slice
{String} optionalDefault: 'router'
Store's key, that will contain router
's data. Use the same one you've used in createRouter
setting up your store.
<Link>
Supports all default <a>
properties.
to
{String|Object}string
type will be interpreted as path or url (external urls are supported as well)object
type can be interpreted 2 ways: if it has property id
, it will be interpreted as route
(see actions.goToRoute), otherwise it will be considered as a standard location object (see actions.push).className
{String} optionalDefault: 'link'
className
for the generated link.
activeClass
{String} optionalDefault: 'link__active'
className
that will be added to the link if it matches current route.
target
{String|Null} optionalDefault: null
Target attribute.
activeMatch
false|'exact'|'partial'|{RegExp} optionalDefault: false
Dictates whether and how activeClass
should be added to the link.
false
- no current route matching at all. This is the default behavior.'exact'
- link will receive its activeClass
only if its pathname
, query
and hash
match current route's.'partial'
- link will receive its activeClass
when current route's pathname
begins with the pathname
supplied to link.'{RegExp}'
- if you supply a regular expression, current route's entire path
will be tested against it.onClick
{Function} optionalDefault: undefined
Optional onClick
callback, that will be fired before <Link>
dispatches its navigation action. If this callback returns a {Promise}
, <Link>
's navigation action will be fired, when the returned {Promise}
resolves.
1// Navigates to /application 2<Link to="/application" activeMatch="exact">Main page</Link> 3 4// Navigates to /application/news?id=1#comment-box 5<Link to={{ pathname: '/application/news', query: { id: '1' }, hash: 'comment-box' }}>News</Link> 6 7// if route is defined like { id: 'Settings', pattern: '/user/:userId' } navigates to /user/1?edit=true#title 8<Link to={{ id: 'Settings', params: { userId: '1' }, query: { edit: true }, hash: '#title' }}></Link>
<Fragment>
Displays react components and other <Fragment>
s based on current route.
id
{String}Route's ID that you want a <Fragment>
displayed on.
redirect
{String|Object} optionalRedirect to path
, url
or route
, when id
is the last in the chain of nested <Fragment>'
s ids.
See <Link>
's to
prop.
component
{React component} optionalYou can pass react component to be used as a direct child of the <Fragment>
.
1<Fragment id="Main"> 2 Main component for my application. 3 4 <Fragment id="News"> 5 Component with my news list. 6 </Fragment> 7 8 <Fragment id="Gallery" component={Gallery} /> // imported <Gallery> react component 9 10 <Fragment id="Contacts"> 11 <Fragment id="Map" component={Map} /> // imported <Map> react component 12 </Fragment> 13 14 <Fragment id="Redirect" redirect={{ id: 'Redirected' }} /> 15 <Fragment id="Redirected"> 16 You have been redirected here. 17 </Fragment> 18 19</Fragment>
We provide a basic example of working React app that you can dig into. Just clone this repo and run:
1npm run build:examples
npm run commit
instead of git commit
.MIT © AuRu
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
Found 12/22 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More