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
Copy file name to clipboardExpand all lines: docs/API.md
+28-14Lines changed: 28 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -171,44 +171,58 @@ It also provides a `router` object on [context](https://facebook.github.io/react
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:
174
+
To use it, you must signal to React that you need it by declaring your use of it in your component via `contextTypes`:
175
175
176
176
```js
177
177
var MyComponent =React.createClass({
178
178
contextTypes: {
179
-
router:Router.PropTypes.router
179
+
router:routerShape.isRequired
180
180
},
181
+
181
182
render:function() {
182
-
//here, you can use `this.context.router`
183
+
//Here, you can use this.context.router.
183
184
}
184
-
});
185
+
})
186
+
```
185
187
188
+
To use `context.router` on a component declared as an ES2015 class, define `contextTypes` as a static property of the class:
189
+
190
+
```js
191
+
classMyComponentextendsReact.Component {
192
+
render() {
193
+
// Here, you can use this.context.router.
194
+
}
195
+
}
196
+
197
+
MyComponent.contextTypes= {
198
+
router:routerShape.isRequired
199
+
}
186
200
```
187
201
188
-
Using `context.router` in combination with ES6 classes requires a different pattern (note the use of the `static` keyword):
202
+
If you are using the class properties proposal, you can instead write:
189
203
190
204
```js
191
205
classMyComponentextendsReact.Component {
192
206
static contextTypes = {
193
-
router:Router.PropTypes.router
207
+
router:routerShape.isRequired
194
208
}
195
209
196
-
render:function() {
197
-
//here, you can use `this.context.router`
210
+
render() {
211
+
//Here, you can use this.context.router.
198
212
}
199
-
});
200
-
213
+
}
201
214
```
202
215
203
-
Finally, you can use `context.router` with
204
-
[stateless function components](https://facebook.github.io/react/docs/reusable-components.html#stateless-functions):
216
+
To use `context.router` with
217
+
[stateless function components](https://facebook.github.io/react/docs/reusable-components.html#stateless-functions), declare `contextTypes` as a static property of the component function:
0 commit comments