Skip to content

Commit dfa1884

Browse files
committed
more docs
1 parent e56a117 commit dfa1884

File tree

2 files changed

+76
-112
lines changed

2 files changed

+76
-112
lines changed

CHANGES.md

Lines changed: 74 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
1-
## [HEAD]
1+
## [HEAD]: https://github.com/rackt/react-router/compare/latest...HEAD
22

3-
### Changes to context exports and Mixins
3+
### Notes
44

5-
Only an object named `router` is added to context. Accessing `context.history`,
6-
`context.location`, and `context.route` are all deprecated.
5+
#### Goals of this release
76

8-
Additionally, since `context` is now documented, all mixins are deprecated.
7+
1. Clear up the coupling between History and Router with sipler APIs.
8+
9+
2. Provide cleaner integrations with other libraries like Redux, Relay, Async Props etc.
10+
11+
3. Stop providing API that conceals usage of `context`. It is now a documented feature of React so developers using Router can implement their own opinions on how best to use context: Mixins, higher-order components, decorators, etc. React Router no longer has an opinion, but instead uses the lowest level feature of React. This project wants to be an incredibly useful routing library and doesn't want to get hung up on best-practice patterns for getting stuff from up top to down low.
12+
13+
#### Backwards Compatibility and Deprecation Warnings
14+
15+
This has been a community project from the start, we need your help making the upgrade as smooth as possible for everybody!
16+
17+
We have done our best to provide backwards compatibility with deprecated APIs. If you drop in v2.x into a v1.x application and it doesn't run, then there is a bug. Please open an issue when you discover what the problem is so we can get a fix out.
18+
19+
The deprecation warnings should also lead you to the relevant part of this document. If one doesn't, please open a pull request with a fix. Also, if any part of this document could be improved, please let us know how. Confound it, our bias is often inescapable!
20+
21+
### New Router render prop (`RoutingContext` prop replacement)
22+
23+
You can now pass a `render` prop to `Router` for it to use for rendering. This allows you to create "middleware components" that participate in routing. Its critical for integrations with libraries like Relay, Redux, Resolver, Transmit, Async Props, etc.
24+
25+
```js
26+
// the default is basically this:
27+
<Router render={props => <RouterContext {...props}/>}/>
28+
```
29+
30+
`RoutingContext` was undocumented and therefore has no backwards compatibility.
31+
32+
### Changes to provided context and Mixins
33+
34+
Only an object named `router` is added to context. Accessing `context.history`, `context.location`, and `context.route` are all deprecated.
35+
36+
Additionally, since `context` is now documented, all mixins are deprecated as they simply served to conceal usage of context.
937

1038
#### Accessing location
1139

@@ -27,13 +55,18 @@ const RouteComponent = React.createClass({
2755
})
2856
```
2957

30-
Though you may want to hide that in a mixin or higher order component,
31-
it's up to you.
58+
### History Mixin
59+
(see below)
3260

33-
#### Navigating
61+
### Route component `history` prop
62+
(see below)
3463

35-
In all cases where you once had a `history` for navigation, you now have a
36-
`router` to navigate instead.
64+
### `context.history`
65+
(see below)
66+
67+
### Programatic Navigation
68+
69+
There were several ways to get a hold of the history object to navigate around (see above :P) 1.0. In 2.0, where you once had a `history`, you now have a `router` to navigate instead (only from context) with a better signature.
3770

3871
```js
3972
// v1.0.x
@@ -48,29 +81,7 @@ router.replace(path)
4881
router.replace({ pathname, query, state }) // new "location descriptor"
4982
```
5083

51-
#### Links
52-
53-
`<Link to>`s now takes a location descriptor. The other props are deprecated.
54-
55-
```js
56-
// v1.0.x
57-
<Link to="/foo" query={{ the: 'query' }}>
58-
59-
// v2.0.0
60-
<Link to={{ pathname: '/foo', query: { the: 'query' } }}>
61-
```
62-
63-
For custom link-like components, the same applies for `router.isActive`.
64-
65-
```js
66-
// v1.0.x
67-
router.isActive(pathname, query, indexOnly)
68-
69-
// v2.0.0
70-
router.isActive({ pathname, query }, indexOnly)
71-
```
72-
73-
#### Navigating in route components
84+
#### Navigating in Route Components
7485

7586
```js
7687
// v1.0.x
@@ -82,13 +93,17 @@ class RouteComponent extends React.Component {
8293

8394
// v2.0.0
8495
class RouteComponent extends React.Component {
96+
contextTypes: {
97+
router: object.isRequired
98+
},
99+
85100
someHandler() {
86-
this.props.router.push(...)
101+
this.context.router.push(...)
87102
}
88103
}
89104
```
90105

91-
#### Navigating inside deeply nested components
106+
### Navigating inside deeply nested components
92107

93108
```js
94109
// v1.0.x
@@ -102,45 +117,26 @@ const DeepComponent = React.createClass({
102117

103118
// v2.0.0
104119
// You have a couple options:
105-
// Use context directly (recommended)
120+
// 1) Use context.router
106121
const DeepComponent = React.createClass({
107122
contextTypes: {
108123
router: object.isRequired
109124
},
110-
111125
someHandler() {
112126
this.context.router.push(...)
113127
}
114128
}
115129

116-
// create your own mixin:
117-
const RouterMixin = {
118-
contextTypes: {
119-
router: object.isRequired
120-
},
121-
componentWillMount() {
122-
this.router = this.context.router
123-
}
124-
}
125-
126-
const DeepComponent = React.createClass({
127-
mixins: [ RouterMixin ],
128-
someHandler() {
129-
this.router.push(...)
130-
}
131-
}
132-
133-
// use the singleton history you are using when the router was rendered,
130+
// 2) Use the singleton history you are using when the router was rendered,
134131
import { browserHistory } from 'react-router'
135-
136132
const DeepComponent = React.createClass({
137133
someHandler() {
138134
browserHistory.push(...)
139135
}
140136
}
141137
```
142138
143-
#### Lifecycle Mixin with route components
139+
### Lifecycle Mixin
144140
145141
```js
146142
// v1.0.x
@@ -153,77 +149,44 @@ const RouteComponent = React.createClass({
153149

154150
// v2.0.0
155151
const RouteComponent = React.createClass({
152+
contextTypes: {
153+
router: object.isRequired
154+
},
156155
componentDidMount() {
157-
const { router, route } = this.props
156+
const { route } = this.props
157+
const { router } = this.context
158158
router.setRouteLeaveHook(route, this.routerWillLeave)
159159
}
160160
})
161-
162-
// or make your own mixin, check it out in the next section
163161
```
164162
165-
You don't need to manually tear down the route leave hook in most cases any
166-
more. We automatically remove all attached route leave hooks after leaving the
167-
associated route.
163+
You don't need to manually tear down the route leave hook in most cases. We automatically remove all attached route leave hooks after leaving the associated route.
164+
165+
### Links `to` Type
168166
169-
#### Lifecycle Mixin with deep, non-route components
167+
`<Link to>` can now take a location descriptor in addition to strings. The `query` and `state` props are deprecated.
170168
171169
```js
172170
// v1.0.x
173-
const DeepComponent = React.createClass({
174-
mixins: [ Lifecycle ],
175-
routerWillLeave() {
176-
// do stuff
177-
}
178-
})
171+
<Link to="/foo" query={{ the: 'query' }}/>
179172

180173
// v2.0.0
181-
// you have a couple of options
182-
// first you can put the route on context in the route component
183-
const RouteComponent = React.createClass({
184-
childContextTypes: {
185-
route: object
186-
},
174+
<Link to={{ pathname: '/foo', query: { the: 'query' } }}/>
187175

188-
getChildContext() {
189-
return { route: this.props.route }
190-
}
191-
})
176+
// Still valid in 2.x
177+
<Link to="/foo"/>
178+
```
192179
193-
// and then access it on your deep component
194-
const DeepComponent = React.createClass({
195-
contextTypes: {
196-
route: object.isRequired,
197-
router: objec.isRequired
198-
},
180+
For custom link-like components, the same applies for `router.isActive`, previously `history.isActive`.
199181
200-
componentDidMount() {
201-
const { router, route } = this.context
202-
router.setRouteLeaveHook(route, this.routerWillLeave)
203-
}
204-
})
205-
206-
// or make your own mixin that will work for both route components and
207-
// deep components alike (as long as your route component puts `route`
208-
// on context
209-
const Lifecycle = {
210-
contextTypes: {
211-
route: object.isRequired,
212-
router: objec.isRequired
213-
},
182+
```js
183+
// v1.0.x
184+
router.isActive(pathname, query, indexOnly)
214185

215-
componentDidMount() {
216-
const router = this.context.router
217-
const route = this.props.route || this.context.route
218-
router.setRouteLeaveHook(route, this.routerWillLeave)
219-
}
220-
}
186+
// v2.0.0
187+
router.isActive({ pathname, query }, indexOnly)
221188
```
222189
223-
### Router render prop
224-
225-
Just a stub so we don't forget to talk about it.
226-
227190
### `RoutingContext` renamed to `RouterContext`
228191
229192
```js
@@ -233,7 +196,6 @@ import { RoutingContext } from 'react-router'
233196
import { RouterContext } from 'react-router'
234197
```
235198
236-
[HEAD]: https://github.com/rackt/react-router/compare/latest...HEAD
237199
238200
## [v1.0.3]
239201
> Dec 23, 2015
@@ -593,7 +555,7 @@ For example, `params` is not available via context.
593555
We're developing scroll behaviors separately in the
594556
[`scroll-behavior`](https://github.com/rackt/scroll-behavior)
595557
library until we have a stable, robust implementation that we're happy with.
596-
Currently, scroll behaviors are exposed there as history enhancers:
558+
Currently, scroll behaviors are exposed there as history enhancers:
597559
598560
```js
599561
import createHistory from 'history/lib/createBrowserHistory'

docs/API.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ Called whenever the router updates its state in response to URL changes.
8585
##### `render(props)`
8686
This is primarily for integrating with other libraries that need to participate in rendering before the route components are rendered. It defaults to `render={(props) => <RouterContext {...props}/>}`.
8787

88+
Ensure that you render a `RouterContext` at the end of the line, passing all the props passed to `render`.
89+
8890

8991
#### Examples
9092
Please see the [`examples/`](/examples) directory of the repository for extensive examples of using `Router`.

0 commit comments

Comments
 (0)