Skip to content

this.context.router is undefined in ES6 class constructor #1059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
torarnek opened this issue Apr 9, 2015 · 7 comments
Closed

this.context.router is undefined in ES6 class constructor #1059

torarnek opened this issue Apr 9, 2015 · 7 comments

Comments

@torarnek
Copy link
Contributor

torarnek commented Apr 9, 2015

this.context.router is accessible in getInitalState(), however, it is undefined in

class MyComp extends React.Component{
  constructor(props){
    super(props);
    this.context.router // is undefined
  }
}

MyComp.contextTypes = {
    router: React.PropTypes.func.isRequired
};

My workaround is to do the init in componentDidMount(), however, that is weird...

@nukr
Copy link

nukr commented Apr 9, 2015

class MyComp extends React.Component{
  constructor(props, context){
    super(props);
    context.router // will work
  }
}

MyComp.contextTypes = {
    router: React.PropTypes.func.isRequired
};

@torarnek
Copy link
Contributor Author

torarnek commented Apr 9, 2015

Fantastic. Thanks!

@ryanflorence
Copy link
Member

should also super(props, context)

note in the 1.0 API you don't need to ask for the router off of context.

@juhaelee
Copy link

@ryanflorence can you explain and point to the api what you mean by you don't need to ask for the router off of the context. Does that mean you don't have to access the context by context.router? How else would you access the router?

@tygriffin
Copy link

+1 for @juhaelee's question. Can we access the router somehow without using context if we're using the class syntax? Or @ryanflorence, did you mean we don't need context if we're using the mixin with the createComponent syntax?

@BabakMN
Copy link

BabakMN commented Jul 8, 2015

I'd also like some clarification about accessing router in 1.0 (when using ES6)

@agundermann
Copy link
Contributor

I guess he was referring to #1173 . There's no implementation yet, so I'd just use context for now.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants