Skip to content

Instantly share code, notes, and snippets.

@eplawless
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eplawless/b17ba00bd3e15cc121a5 to your computer and use it in GitHub Desktop.
Save eplawless/b17ba00bd3e15cc121a5 to your computer and use it in GitHub Desktop.
react router injection
function getNavigation({ React }) {
var { object } = React.PropTypes;
/**
* A mixin for components that modify the URL.
*
* Example:
*
* import { Navigation } from 'react-router';
*
* var MyLink = React.createClass({
* mixins: [ Navigation ],
* handleClick(event) {
* event.preventDefault();
* this.transitionTo('aRoute', { the: 'params' }, { the: 'query' });
* },
* render() {
* return (
* <a onClick={this.handleClick}>Click me!</a>
* );
* }
* });
*/
var Navigation = {
contextTypes: {
router: object.isRequired
}
};
var RouterNavigationMethods = [
'makePath',
'makeHref',
'transitionTo',
'replaceWith',
'go',
'goBack',
'goForward'
];
RouterNavigationMethods.forEach(function (method) {
Navigation[method] = function () {
var router = this.context.router;
return router[method].apply(router, arguments);
};
});
return Navigation;
}
export default getNavigation;
import getNavigation from './getNavigation';
import React from 'react';
export default getNavigation({ React });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment