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

TransitionHook alternative to be used with ES6 classes #1499

Closed
eriknyk opened this Issue Jul 8, 2015 · 5 comments

Comments

4 participants
@eriknyk

eriknyk commented Jul 8, 2015

Please can you tell me how I can use TransitionHook with ES6 classes, since mixins are deprecated
for ES6 classes.

Regards.

@taurose

This comment has been minimized.

Contributor

taurose commented Jul 8, 2015

Looking at https://github.com/rackt/react-router/blob/master/modules/TransitionHook.js it should work like this:

class RouteComponent extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.routerWillLeave = this.routerWillLeave.bind(this);
  }

  componentDidMount() {
    this.context.router.addTransitionHook(this.routerWillLeave);
  }

  componentWillUnmount() {
      this.context.router.removeTransitionHook(this.routerWillLeave);
  }

  routerWillLeave (nextState, router) {

  }
}

RouteComponent.contextTypes = {
  router: React.PropTypes.object.isRequired
}
@BabakMN

This comment has been minimized.

BabakMN commented Jul 9, 2015

@taurose have you achieved this successfully in ES6? For me this.context.router is undefined.

@BabakMN

This comment has been minimized.

BabakMN commented Jul 9, 2015

Nevermind I worked it out. I was trying to have contextTypes inside the class definition, doing it as @taurose has demonstrated works.

@jedrichards

This comment has been minimized.

jedrichards commented Jul 9, 2015

I think it should work with ES7 static property initialisers too.

class RouteComponent extends React.Component {
  static contextTypes = {
    router: React.PropTypes.object.isRequired
  };
}
@eriknyk

This comment has been minimized.

eriknyk commented Jul 13, 2015

thank you to everybody!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment