Installations
npm install @superwf/mobx-react-router
Developer Guide
Typescript
Yes
Module System
CommonJS, UMD
Node Version
16.13.0
NPM Version
8.1.0
Score
71.9
Supply Chain
98.9
Quality
75.5
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (81.48%)
JavaScript (18.52%)
Developer
superwf
Download Statistics
Total Downloads
325,357
Last Day
65
Last Week
646
Last Month
4,422
Last Year
65,839
GitHub Statistics
13 Stars
63 Commits
9 Forks
1 Watching
14 Branches
1 Contributors
Package Meta Information
Latest Version
7.4.0
Package Id
@superwf/mobx-react-router@7.4.0
Unpacked Size
185.24 kB
Size
25.11 kB
File Count
12
NPM Version
8.1.0
Node Version
16.13.0
Total Downloads
Cumulative downloads
Total Downloads
325,357
Last day
-74.5%
65
Compared to previous day
Last week
-27.1%
646
Compared to previous week
Last month
-29.2%
4,422
Compared to previous month
Last year
-39.4%
65,839
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
3
Dev Dependencies
45
mobx-react-router
Overview
Keep your MobX state in sync with react-router via a RouterStore
.
Router location state is observable, so any references to it in MobX
components will cause the component to re-render when the location changes.
This repo is forked from alisd23/mobx-react-router.
Totally rewrite with typescript and has type definition together.
Tested 🥳
Upgrade note
💡 Note 2021-8-16 update to v7 for compatible with history v5, mobx v6, react-router v5 and path-to-regexp v6
Since History
upgrade to V5, its api changed a lot.
So please READ API part again even you have used mobx-react-router.
💡 Note if you need to work woth old version react-router and history, install v6 by npm install --save @superwf/mobx-react-router@6.0.0
Installation
1npm install --save @superwf/mobx-react-router
Example
Complete code here: example
router.js
1import { createBrowserHistory } from 'history' 2import { RouterStore } from '@superwf/mobx-react-router' 3 4const browserHistory = createBrowserHistory() 5export const router = new RouterStore(browserHistory)
index.js
1import React from 'react' 2import ReactDOM from 'react-dom' 3import { Router } from 'react-router' 4import App from './App' 5import { router } from './router' 6 7ReactDOM.render( 8 <Router history={router.history}> 9 <App /> 10 </Router> 11 document.getElementById('root') 12)
App.js
1import React, { Component } from 'react' 2import { observer } from 'mobx-react-lite' 3import { router } from './router' 4 5export const App = observer(() => { 6 const { location, push, back } = router 7 return ( 8 <div> 9 <span>Current pathname: {location.pathname}</span> 10 <button onClick={() => push('/test')}>go to "/test"</button> 11 <button onClick={back}>Go Back</button> 12 </div> 13 ) 14})
CDN
- Global var mode mode. Global variable name:
window.MobxReactRouter
<script type="javascript" src="https://unpkg.com/@superwf/mobx-react-router@latest/dist/mobx-react-router.min.js"></script>
- Es module mode.
1import { RouterStore } from 'https://unpkg.com/@superwf/mobx-react-router/module/index.js'
API
RouterStore constructor
param: history
- A variant of a history object, usually browserHistory
1const browserHistory = createBrowserHistory() 2// or hashHistory or memoryHistory 3const router = new RouterStore(browserHistory)
Instance properties
A RouterStore instance has the following properties:
- history - raw history API object
state
(observable) - sync with history state, type as below.
1{ action: history.action, location: history.location }
- location (observable, readonly) - sync with history location
1router.push('/test1') 2router.location.pathname // '/test1'
-
history the history instance from constructor. Use it as your will, do not update it.
-
pathList string[], observable, used to match
pathValue
. Do not use it directly unless you absolutely know your purpose. -
query url search object format.
1router.push('/abc?a=1&b=2') 2router.query // { a: '1', b: '2' } 3router.push('/abc?id=1&id=2') 4router.query // { id: ['1', '2'] }
- hashValue hash string without
#
.
1router.push('/abc#xxx') 2router.hashValue // 'xxx'
- pathValue extract path parameter to object type, need
pathList
work together.
1router.appendPathList('/user/:name') 2router.push('/user/xxx') 3router.hashValue // 'xxx'
Instance methods
- stopSyncWithHistory, stop sync with history any more, once stoped, can not start again.
1router.push('/test1') 2router.location.pathname // '/test1' 3router.stopSyncWithHistory() 4router.push('/test2') // not sync any more 5router.location.pathname // '/test1'
- subscribe(listener) and unsubscribe()
Subscribes to any changes in the store's location
observable,
and run the listener at once with current history state.
Returns an unsubscribe function which destroys the listener
1const stopListen = router.subscribe(({ location }) => console.log(location.pathname)) 2router.push('/test1') // output '/test1' 3stopListen() 4router.push('/test2') // no output any more
- appendPathList, prependPathList
Append or prepend new paths to pathList
property,
💡 Note path in pathList order is important, first matched path will return the pathValue
result.
Use prependPathList
for some path
which has high priority.
1router.appendPathList('/user/:name') 2router.push('/user/rock') // match "/user/:name" 3router.pathValue // now get a path param: { name: 'rock' }
The following methods bind to the history instance, for more detail read here: history methods:
- push
- replace
- go
- back
- forward
- subscribe
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 1/29 approved changesets -- score normalized to 0
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
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 2 are checked with a SAST tool
Reason
24 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
1.7
/10
Last Scanned on 2024-12-16
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