2015-08-05 15:35:34 +02:00
|
|
|
---
|
|
|
|
title: React-router
|
2015-11-24 06:09:17 +01:00
|
|
|
category: React
|
2015-08-05 15:35:34 +02:00
|
|
|
---
|
|
|
|
|
2023-03-13 13:02:33 +01:00
|
|
|
### About
|
|
|
|
{: .-intro}
|
|
|
|
|
|
|
|
- <https://reactrouter.com/>
|
|
|
|
|
2017-08-28 17:51:51 +02:00
|
|
|
{% raw %}
|
|
|
|
|
2015-08-05 15:35:34 +02:00
|
|
|
### Basic
|
|
|
|
|
|
|
|
```js
|
|
|
|
import { default as Router, Route } from 'react-router'
|
|
|
|
|
|
|
|
const routes = (
|
|
|
|
<Route>
|
|
|
|
<Route path='*' handler={RootView} />
|
|
|
|
</Route>
|
|
|
|
)
|
|
|
|
|
|
|
|
Router.run(routes, Router.HashLocation, (Root) => {
|
|
|
|
React.render(<Root />, document.getElementById('all'))
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
### Nesting
|
|
|
|
|
|
|
|
```js
|
|
|
|
const routes = (
|
2015-09-09 13:17:04 +02:00
|
|
|
<Route handler={Chrome}>
|
2015-08-05 15:35:34 +02:00
|
|
|
<Route path='about' handler={About} />
|
|
|
|
<Route path='inbox' handler={Inbox} />
|
|
|
|
<Route path='messages/:id' handler={Message} />
|
|
|
|
</Route>
|
|
|
|
)
|
|
|
|
|
|
|
|
import { RouteHandler } from 'react-router'
|
|
|
|
|
|
|
|
const Chrome = React.createClass({
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<h1>App</h1>
|
|
|
|
<RouteHandler />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
### URL params
|
|
|
|
|
|
|
|
```js
|
|
|
|
var Message = React.createClass({
|
|
|
|
componentDidMount: function () {
|
|
|
|
// from the path `/inbox/messages/:id`
|
|
|
|
var id = this.props.params.id
|
|
|
|
...
|
|
|
|
```
|
|
|
|
|
|
|
|
### Link
|
|
|
|
|
|
|
|
```js
|
|
|
|
import { Link } from 'react-router'
|
|
|
|
|
|
|
|
<!-- make a named route `user` -->
|
|
|
|
<Link to='user' params={{userId: 10}} />
|
|
|
|
|
|
|
|
<Link to='login'
|
|
|
|
activeClassName='-active'
|
|
|
|
onClick='...'>
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
### Other config
|
|
|
|
|
|
|
|
```js
|
|
|
|
<Route path='/'>
|
|
|
|
<DefaultRoute handler={Home} />
|
|
|
|
<NotFoundRoute handler={NotFound} />
|
|
|
|
|
|
|
|
<Redirect from='login' to='sessions/new' />
|
|
|
|
<Redirect from='login' to='sessions/new' params={{from: 'home'}} />
|
|
|
|
<Redirect from='profile/:id' to='about-user' />
|
|
|
|
|
|
|
|
<Route name='about-user' ... />
|
|
|
|
```
|
|
|
|
|
|
|
|
### Router.create
|
|
|
|
|
|
|
|
```js
|
|
|
|
var router = Router.create({
|
|
|
|
routes: <Route>...</Route>,
|
|
|
|
location: Router.HistoryLocation
|
|
|
|
})
|
|
|
|
|
|
|
|
router.run((Root) => { ... })
|
|
|
|
```
|
|
|
|
|
|
|
|
### Navigation
|
|
|
|
|
|
|
|
```js
|
|
|
|
import { Navigation } from 'react-router'
|
|
|
|
|
|
|
|
React.createClass({
|
|
|
|
mixins: [ Navigation ], ...
|
|
|
|
})
|
|
|
|
|
|
|
|
this
|
|
|
|
.transitionTo('user', {id: 10})
|
|
|
|
.transitionTo('/path')
|
|
|
|
.transitionTo('http://...')
|
|
|
|
.replaceWith('about')
|
|
|
|
.makePath('about') // return URL
|
|
|
|
.makeHref('about') // return URL
|
|
|
|
.goBack()
|
|
|
|
```
|
2017-08-28 17:51:51 +02:00
|
|
|
|
|
|
|
{% endraw %}
|