Skip to content

Instantly share code, notes, and snippets.

@pekkis
Created June 6, 2017 11:23
Show Gist options
  • Save pekkis/ec817fb94a82398ec34d274df4b76d3d to your computer and use it in GitHub Desktop.
Save pekkis/ec817fb94a82398ec34d274df4b76d3d to your computer and use it in GitHub Desktop.
import React from 'react';
import { Router } from 'react-router-dom';
import createBrowserHistory from 'history/createBrowserHistory';
const LOCATION_CHANGE = '@@router/LOCATION_CHANGE';
export const history = createBrowserHistory();
class ReduxRouter extends React.Component {
static propTypes = {
children: React.PropTypes.node.isRequired,
dispatch: React.PropTypes.func.isRequired,
};
componentDidMount() {
this.unsubscribe = history.listen(this.handleLocationChange);
}
componentWillUnmount() {
this.unsubscribe();
}
handleLocationChange = (location, action) => {
const { dispatch } = this.props;
dispatch({
type: LOCATION_CHANGE,
location,
action,
});
};
render() {
const { children } = this.props;
return (
<Router history={history}>
{children}
</Router>
);
}
}
export const { push, replace, go, goBack, goForward } = history;
export default ReduxRouter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment