From 6f81b73984f107ceb31e97a733f49dfddfdbda4c Mon Sep 17 00:00:00 2001 From: Stijn de Witt Date: Mon, 18 Apr 2016 13:02:48 +0200 Subject: [PATCH 1/2] 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 --- docs/API.md | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/docs/API.md b/docs/API.md index 6e6d2895ae..ba42a2c249 100644 --- a/docs/API.md +++ b/docs/API.md @@ -165,12 +165,53 @@ An `` is like a [``](#link), except it is only active when the ### `` A `` renders the component tree for a given router state. Its used by `` but also useful for server rendering and integrating in brownfield development. -It also provides a `router` object on `context`. +It also provides a `router` object on [context](https://facebook.github.io/react/docs/context.html). #### `context.router` Contains data and methods relevant to routing. Most useful for imperatively transitioning around the application. +To use it, you must signal to React that you need it by declaring your use of it in your component: + +```js +var MyComponent = React.createClass({ + contextTypes: { + router: React.PropTypes.object.isRequired + }, + render: function() { + // here, you can use `this.context.router` + } +}); + +``` + +Using `context.router` i.c.w ES6 classes requires a different pattern (note the use of the `static` keyword): + +```js +class MyComponent extends React.Component { + static contextTypes = { + router: React.PropTypes.object.isRequired + } + + render: function() { + // here, you can use `this.context.router` + } +}); + +``` + +Finally, you can use `context.router` with +[stateless function components](https://facebook.github.io/react/docs/reusable-components.html#stateless-functions): + +```js +function MyComponent(props, context) { + // here, you can use `context.router` +} +MyComponent.contextTypes = { + router: React.PropTypes.object.isRequired +} +``` + ##### `push(pathOrLoc)` Transitions to a new URL, adding a new entry in the browser history. From 681b43381fe79a132b3b4da1716a0cddf8b822ee Mon Sep 17 00:00:00 2001 From: Stijn de Witt Date: Mon, 18 Apr 2016 21:45:06 +0200 Subject: [PATCH 2/2] 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' https://github.com/reactjs/react-router/pull/3346 --- docs/API.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/API.md b/docs/API.md index ba42a2c249..a2bfdbd442 100644 --- a/docs/API.md +++ b/docs/API.md @@ -176,7 +176,7 @@ To use it, you must signal to React that you need it by declaring your use of it ```js var MyComponent = React.createClass({ contextTypes: { - router: React.PropTypes.object.isRequired + router: Router.PropTypes.router }, render: function() { // here, you can use `this.context.router` @@ -185,12 +185,12 @@ var MyComponent = React.createClass({ ``` -Using `context.router` i.c.w ES6 classes requires a different pattern (note the use of the `static` keyword): +Using `context.router` in combination with ES6 classes requires a different pattern (note the use of the `static` keyword): ```js class MyComponent extends React.Component { static contextTypes = { - router: React.PropTypes.object.isRequired + router: Router.PropTypes.router } render: function() { @@ -208,7 +208,7 @@ function MyComponent(props, context) { // here, you can use `context.router` } MyComponent.contextTypes = { - router: React.PropTypes.object.isRequired + router: Router.PropTypes.router } ```