React-router: Add instruction for how to get context of router

Created on 3 Feb 2016  Â·  4Comments  Â·  Source: ReactTraining/react-router

I recently use ES6 style to develop React project with react-router(2.0.0-rc5). So I used the context.router instead of history by reading the guide and CHANGELOG.

However, some error happened.

First, the this.context.router is undefined, and I found answer in rackt/react-router#975 and rackt/react-router#1059 .

Second, some warning appeared in console:

Warning: Failed Context Types: Invalid context `router` of type `object` supplied to `Banner`, expected `function`. Check the render method of `Home`.

and I fix it by react-bootstrap/react-router-bootstrap#91

Could you add some instruction such as ES6 solution for these problems to docs, please ? (⊙v⊙)

Most helpful comment

Use contextTypes like this:

class Index extends Component {

  static contextTypes = {
    router: PropTypes.object.isRequired
  };
//...
}

All 4 comments

And this is exactly why we don't do support on the issue tracker – all of those instructions are old, and pertain to stuff that's like 2 major releases old, or older.

@taion anyway, thanks

Use contextTypes like this:

class Index extends Component {

  static contextTypes = {
    router: PropTypes.object.isRequired
  };
//...
}

using ES7 property initialisers need to install babel-plugin-transform-class-properties

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hgezim picture hgezim  Â·  3Comments

Waquo picture Waquo  Â·  3Comments

alexyaseen picture alexyaseen  Â·  3Comments

misterwilliam picture misterwilliam  Â·  3Comments

tomatau picture tomatau  Â·  3Comments