Created
June 6, 2017 11:23
-
-
Save pekkis/ec817fb94a82398ec34d274df4b76d3d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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