react-mobx-routing v1.0.16
react-mobx-routing
This package provides you the browser routing for:
Installation
npm
npm i react-mobx-routingyarn
yarn add react-mobx-routingThe simplest way of using is Create React App.
Router
Use Router anywhere to show content by URL matching.
import Router, {history} from 'react-mobx-routing'
// or
// import Router, {history} from 'react-mobx-routing/Router'
const App = () => (
<div>
<button onClick={() => history.push('/')}>home</button> |
<button onClick={() => history.push('/test')}>test</button>
<div>
This is
<Router path='/'>
home
</Router>
<Router path='/test'>
test
</Router>
page
</div>
</div>
)The history is mobx-history-api.
path 
Use path to show router content by URL path
const Test = () => <Router path='/test'>test</Router>
testwill be shown when url equals/testor/test?key=value#testbut not for/test/420or/user/test.
You can use it as regexp.
const Test = () => <Router path='/(foo|bar)'>test</Router>
testwill be shown when url path equals/fooor/bar.
You can get foo or bar by children function
const Test = () => <Router path='/(foo|bar)'>{get => get(1)}</Router>
/fooreturnsfooand/barreturnsbar.
The number in the get function says which brackets you want to use.
const Test = () => <Router path='/(foo|bar)/(\d+)'>{get => get(2)}</Router>
/foo/13returns13and/bar/420returns420.
match 
Use match if you want to match URL by custom regexp
const Test = () => <Router match='^/(foo|bar)'>FOOBAR</Router>
/foo/13returnsFOOBARand/barreturnsFOOBAR.
If you use match then path, search, hash, ish, pathIsh, searchIsh and hashIsh are not be used.
You can use a function as a child to get the value of the matching like for path.
pathIsh 
Use pathIsh to make the soft routing by path. That means the path should start with path property.
const Test = () => <Router path='/(foo|bar)' pathIsh>FOOBAR</Router>
/foo/13returnsFOOBARand/bar/420/test?key=value#testreturnsFOOBAR.
Starts with/fooor/bar.
ish 
Use ish instead of pathIsh, searchIsh and hashIsh equal true
const Test = () => <Router path='/(foo|bar)' ish>FOOBAR</Router>The same as pathIsh
search 
Use search if you want to show content by search query of URL.
const Test = () => <Router search='key=value'>test</Router>
/foo/13?key=value#420returnstestbut/foo/13?key=value&testreturns empty content.
searchIsh 
Use searchIsh or ish to make a soft search.
const Test = () => <Router search='key=value' ish>test</Router>now
/foo/13?key=value&testand/foo/13?test=1&key=value&foo=barreturnstest.
Also, you can use only key for search
const Test = () => <Router search='key' ish>test</Router>
/?key&valueand/?value&keyreturnstestbut/?key=1and/?key1returns nothing.
hash 
Use hash if you want to show content by hash of URL.
const Test = () => <Router hash='test'>test</Router>
/any/path?any=search#testreturnstestbut/#test1returns empty content.
hashIsh 
Use hashIsh or ish to fix it.
const Test = () => <Router hash='test' ish>test</Router>now
/#test1and/#sometextwiththetestwordreturnstest.
other 
This is an alternative of react Switch.Router with other shows content only if all routers without other in the same Router are not matched.
const Test = () => (
<Router>
<Router path='/'>home</Router>
<Router path='/user'>user</Router>
<Router other>other</Router>
</Router>
)will show
homefor/,userfor/userandotherfor any other url
You may use any structure inside Router and several other routers with any props.
const Test = () => (
<Router>
<div>
<Router path='/'>home</Router>
<div>
content
<Router path='/user'>user</Router>
</div>
<Router search='modal' other>modal</Router>
<Router other>
<Router path='/test'>test</Router>
<Router other><div>other</div></Router>
</Router>
</div>
</Router>
)showDelay 
You can show content of router with delay.
const Test = () => <Router path='/test' showDelay={1000}>test</Router>when URL became
/testthe content be not shown,testwill be shown in a second after that.
hideDelay 
This is the same showDelay but for hiding.
const Test = () => <Router path='/test' hideDelay={1000}>test</Router>when URL became
/testthe content be shown immediately, but when URL is changed after that,testwill be hidden in a second.
delay 
This is the combine of showDelay and hideDelay.
const Test = () => <Router path='/test' delay={1000}>test</Router>
testwill be shown or hidden in a second.
onShow 
It calls any time when the content will be shown
const Test = () => (
<Router
path='/test'
onShow={() => console.log('test')}>
test
</Router>
)onShown 
It calls any time when the content has shown
const Test = () => (
<Router
path='/test'
delay={1000}
onShown={() => console.log('test')}>
test
</Router>
)onHide 
It calls any time when the content will be hidden
const Test = () => (
<Router
path='/test'
onHide={() => console.log('test')}>
test
</Router>
)onHidden 
It calls any time when the content has hidden
const Test = () => (
<Router
path='/test'
delay={1000}
onHidden={() => console.log('test')}>
test
</Router>
)Redirect
Use the component for comfortable redirection
import {Redirect} from 'react-mobx-routing'url 
Use the prop to redirect at the url.
const RedirectToHome = () => (
<Redirect url='/' />
)
const RedirectToLogin = () => (
<Redirect url='?modal=login' />
)
const RedirectToHeader = () => (
<Redirect url='#root' />
)
const RedirectToRepo = () => (
<Redirect url='https://github.com/d8corp/react-mobx-routing' />
)path 
The same as url but works only with path.
const RedirectToHome = () => (
<Redirect path='/' />
)You may combine with url
const RedirectToHome = () => (
<Redirect url='/foo#bar' path='/' />
)
// redirects to /#barsearch

The same as path but works with search and you may combine with url
const RedirectToLoginModal = () => (
<Redirect search='modal=login' />
)
// redirects to ?modal=loginYou may use an object of search keys and values
const RedirectToLoginModal = () => (
<Redirect search={{modal: 'login'}} />
)
// redirects to ?modal=loginundefined value removes the key
history.push('/test?key=value')
render (
<Redirect search={{key: undefined}} />
)
// redirects to /testhash 
The same as path but works with hash and you may combine with url
const RedirectToRoot = () => (
<Redirect hash='root' />
)
// redirects to #rootpush 
By default Redirect replaces url. If you wanna push the redirection to history use the property.
const RedirectToHome = () => (
<Redirect path='/' push />
)position

By default the page scrolls up during redirection. You may change the scroll position by the property.
const RedirectToHome = () => (
<Redirect path='/' position={60} />
)You may scroll to any element by selector query
const RedirectToHome = () => (
<Redirect path='/' position='#root' />
)scrollFirst 
When you use smooth scroll you can wait while the scrolling finished and then make the redirection.
const RedirectToHome = () => (
<Redirect path='/' scrollFirst />
)Link
Use the component instance of a.
rel="noreferrer"andtarget="_blank"are default for external links.href
If
hrefstarts from/then theLinkwill use History API./is default value ofhref.const App = () => ( <> <div> <Link>Home</Link> <Link href='/test'>Test</Link> </div> <Router path='/'>Home</Router> <Router path='/test'>Test</Router> </> )When
hrefstarts from?theLinkwill keep the pathname and change the search and hash.const App = () => ( <> <div> <Link>Home</Link> <Link href='/test'>Test</Link> <Link href='?modal=test'>Test Modal</Link> </div> <Router path='/'>Home</Router> <Router path='/test'>Test</Router> <Router search='modal=test'><div>Test Modal</div></Router> </> )When
hrefstarts from#theLinkwill keep the whole URL except for hash.replace
By default
Linkpushes to history but you may usereplaceto replace current history state.const Agree = () => ( <Link replace href='?'>I agree</Link> )
href='?'means clearing of search and hashactiveClass
If you set
activeClassthen the link will have the class if url starts fromhrefconst Test = () => ( <Link activeClass='active' href='/test'>test</Link> )When you click the link html will be equal
<a class="active" href="/test">test</a>exact
By default
activeClasswill be applied when url starts fromhrefbut useexactto compare exactly.const Test = () => ( <Link activeClass='active' href='/test' exact>test</Link> )scrollTo
![]()
If you wanna scroll the page to custom position (by default it's up of the page) use
scrollToconst To100 = () => ( <Link scrollTo={100} href='/test'>test</Link> )
const ToRoot = () => (