Skip to content

Commit 9936099

Browse files
Downloadtimdorr
authored andcommitted
Expanded API docs for context.router (#3346)
* Expanded API docs for `context.router` Explain usage of `context.router` for: * Components created with `React.createClass` * Components created with ES classes extending `React.Component` * Components created as stateless functions * Changes based on feedback from code review * Replaced occurances of `React.PropTypes.object.isRequired` with `Router.PropTypes.router` * Expanded abbreviation 'i.c.w' to 'in combination with' #3346
1 parent 0b4212d commit 9936099

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

docs/API.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,53 @@ An `<IndexLink>` is like a [`<Link>`](#link), except it is only active when the
165165
### `<RouterContext>`
166166
A `<RouterContext>` renders the component tree for a given router state. Its used by `<Router>` but also useful for server rendering and integrating in brownfield development.
167167

168-
It also provides a `router` object on `context`.
168+
It also provides a `router` object on [context](https://facebook.github.io/react/docs/context.html).
169169

170170
#### `context.router`
171171

172172
Contains data and methods relevant to routing. Most useful for imperatively transitioning around the application.
173173

174+
To use it, you must signal to React that you need it by declaring your use of it in your component:
175+
176+
```js
177+
var MyComponent = React.createClass({
178+
contextTypes: {
179+
router: Router.PropTypes.router
180+
},
181+
render: function() {
182+
// here, you can use `this.context.router`
183+
}
184+
});
185+
186+
```
187+
188+
Using `context.router` in combination with ES6 classes requires a different pattern (note the use of the `static` keyword):
189+
190+
```js
191+
class MyComponent extends React.Component {
192+
static contextTypes = {
193+
router: Router.PropTypes.router
194+
}
195+
196+
render: function() {
197+
// here, you can use `this.context.router`
198+
}
199+
});
200+
201+
```
202+
203+
Finally, you can use `context.router` with
204+
[stateless function components](https://facebook.github.io/react/docs/reusable-components.html#stateless-functions):
205+
206+
```js
207+
function MyComponent(props, context) {
208+
// here, you can use `context.router`
209+
}
210+
MyComponent.contextTypes = {
211+
router: Router.PropTypes.router
212+
}
213+
```
214+
174215
##### `push(pathOrLoc)`
175216
Transitions to a new URL, adding a new entry in the browser history.
176217

0 commit comments

Comments
 (0)