react-router-mobx
When React Router meets MobX: observable router and location.
Table of Contents
Features
location
are observable- Built-in
query
observable object tolocation
- Super easy to push/update new URL, pathname, hash, search or query
WTF
If you wanna push url from http://aweso.me/search?q=hello&page=4
to http://aweso.me/search?q=hello&page=5
, you may need:
Before
;;;;;; @withRouter@observer static propTypes = location: PropTypesobjectisRequired history: PropTypesobjectisRequired ; { ev; const location history = thisprops; const query = qs; history; }; { const location = thisprops; const page = qs; return <div> <div>myStoresomeContent</div> <p>Page: page || 1</p> <button onClick=thisgoToNextPage>Next</button> </div> ; }
After
;;;;; @observer { ev; const location = routerStore; locationquery = ...locationquery page: 1 + locationquerypage ; }; { const page = routerStorelocationquery; return <div> <div>myStoresomeContent</div> <p>Page: page || 1</p> <button onClick=thisgoToNextPage>Next</button> </div> ; }
Installation
yarn add react-router-mobx
You should install all the peer dependencies if you haven't installed them:
yarn add react mobx mobx-react react-router-dom
If you are using React Native, please install react-router-native
instead of react-router-dom
.
Usage
- Use react-router-mobx
Router
instead of react-routerRouter
- Pass a
RouterStore
instance and react-routerRouter
component toRouter
component:
;;; const routerStore = ; { return <Router component=BrowserRouter routerStore=routerStore> <Route ...someRouteConfigs /> </Router> ; }
API Reference
RouterStore
The MobX store class that contains some router properties and methods.
RouterStore#location
A little bits like react-router location
object which contains key
, pathname
, search
, hash
, state
. But there are several differences:
- Prividing
query
object, just like react-router v3 or below - All properties are observable and mutable
- Could push URL by passing new location or properties, just like
window.location
- Push a new URL:
routerStore.location = '/foo?say=hello'
- Push a new
pathname
, i.e. from/foo?say=hello
to/bar?say=hello
:routerStore.location.pathname = '/bar'
- Push a new
search
, i.e. from/foo?say=hello
to/foo?say=world
:routerStore.location.query = { say: 'world' }
orrouterStore.location.search = '?say=world'
- Push a new URL:
RouterStore#history
Just like react-router history
object, except for history.listen
:
history;
RouterStore#push(loc, state)
Like react-router history.push(loc, state)
, but the loc
param supports to be an object that contains a query
object.
RouterStore#replace(loc, state)
Like react-router history.replace(loc, state)
, but the loc
param supports to be an object that contains a query
object.
Router
The low-level api router component instead of react-router Router
component.
Props
- routerStore (RouterStore): Defining a
RouterStore
instance to store or updatelocation
state - component (ReactComponent): Defining the react router component, e.g.
BrowserRouter
,MemoryRouter
,NativeRouter
, etc. Defaults to react-routerRouter
component - history (Object): You can also define a custom history object, just like react-router
Router
component - All properties in react-router
Router
are supported
setQueryString(queryString)
Setting a custom queryString
library.
Arguments
- queryString (Object): Custom
queryString
library, which should containparse(object)
andstringify(object)
methods
Example
;;;
Versioning
This library follows Semantic Versioning.
This library is considered to be General Availability (GA). This means it is stable; the code surface will not change in backwards-incompatible ways unless absolutely necessary (e.g. because of critical security issues) or with an extensive deprecation period. Issues and requests against GA libraries are addressed with the highest priority.
License
MIT