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

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

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

Comments

Projects
None yet
7 participants
@torarnek
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

This comment has been minimized.

Show comment
Hide comment
@nukr

nukr Apr 9, 2015

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

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

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

This comment has been minimized.

Show comment
Hide comment
@torarnek

torarnek Apr 9, 2015

Contributor

Fantastic. Thanks!

Contributor

torarnek commented Apr 9, 2015

Fantastic. Thanks!

@ryanflorence

This comment has been minimized.

Show comment
Hide comment
@ryanflorence

ryanflorence Jun 17, 2015

Contributor

should also super(props, context)

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

Contributor

ryanflorence commented Jun 17, 2015

should also super(props, context)

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

@juhaelee

This comment has been minimized.

Show comment
Hide comment
@juhaelee

juhaelee Jun 22, 2015

@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?

juhaelee commented Jun 22, 2015

@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

This comment has been minimized.

Show comment
Hide comment
@tygriffin

tygriffin Jun 29, 2015

+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?

tygriffin commented Jun 29, 2015

+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

This comment has been minimized.

Show comment
Hide comment
@BabakMN

BabakMN Jul 8, 2015

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

BabakMN commented Jul 8, 2015

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

@taurose

This comment has been minimized.

Show comment
Hide comment
@taurose

taurose Jul 8, 2015

Contributor

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

Contributor

taurose commented Jul 8, 2015

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 join this conversation on GitHub. Already have an account? Sign in to comment