javascript - React Router 1.0 using router.transitionTo() without Navigation mixin -


is there easier way access router object component things call transitionto() without using navigation mixin? es6 component. on event button click, have been writing this:

class button extends react.component {   handleclick(e) {     e.preventdefault();     var router = this._reactinternalinstance._context.router;     router.transitionto('/search');   }    render() {     return (       <button onclick={this.handleclick.bind(this)}>         {this.props.children}       </button>     );   } } 

per mat gessel's comment, adding context parameter in constructor give access router.

class button extends react.component {   constructor(props, context) {     super(props, context);     this.context = context;         }    handleclick(e) {     e.preventdefault();     this.context.router.transitionto('/search');   }    render() {     return (       <button onclick={this.handleclick.bind(this)}>         {this.props.children}       </button>     );   } }  button.contexttypes = {   router: react.proptypes.object.isrequired }; 

Comments