You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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
Copy file name to clipboardExpand all lines: docs/API.md
+42-1Lines changed: 42 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -165,12 +165,53 @@ An `<IndexLink>` is like a [`<Link>`](#link), except it is only active when the
165
165
### `<RouterContext>`
166
166
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.
167
167
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).
169
169
170
170
#### `context.router`
171
171
172
172
Contains data and methods relevant to routing. Most useful for imperatively transitioning around the application.
173
173
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
+
classMyComponentextendsReact.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
+
functionMyComponent(props, context) {
208
+
// here, you can use `context.router`
209
+
}
210
+
MyComponent.contextTypes= {
211
+
router:Router.PropTypes.router
212
+
}
213
+
```
214
+
174
215
##### `push(pathOrLoc)`
175
216
Transitions to a new URL, adding a new entry in the browser history.
0 commit comments