Created
November 1, 2016 22:55
-
-
Save SimeonC/55a75b6d34789a9b668bd63abc0ca1f3 to your computer and use it in GitHub Desktop.
react-router v4 Simple Redux Connect setup.
This file contains 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
export function push(to) { | |
return {type: 'ROUTE_TRANSITION', to, replace: false}; | |
} | |
export function replace(to) { | |
return {type: 'ROUTE_TRANSITION', to, replace: true}; | |
} | |
// Simply import and dispatch these whereever you would use `import {push, replace} from 'react-router-redux';` |
This file contains 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, {Component} from 'react'; | |
import BrowserRouter from 'react-router/BrowserRouter'; | |
import ReduxRouterConnect from './ReduxRouterConnect.jsx'; | |
export default class Index extends Component { | |
render() { | |
return <BrowserRouter> | |
<Other routes go here.../> | |
<ReduxRouterConnect/> | |
</BrowserRouter>; | |
} | |
} |
This file contains 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
export default function reducer(state = {routing: {}}, action) { | |
case 'ROUTE_TRANSITION': | |
return { | |
...state, | |
routing: { | |
to: action.to, | |
replace: action.replace | |
} | |
}; | |
default: | |
return state; | |
} |
This file contains 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, {Component} from 'react'; | |
import {connect} from 'react-redux'; | |
class ReduxRouterConnect extends Component { | |
static contextTypes = { | |
router: React.PropTypes.object | |
}; | |
componentWillReceiveProps(nextProps) { | |
if (nextProps.to !== this.props.to) { | |
if (nextProps.replace) { | |
this.context.router.replaceWith(to); | |
} else { | |
this.context.router.transitionTo(to); | |
} | |
} | |
} | |
render () { | |
return <div/>; | |
} | |
} | |
export default connect(state => ({...state.routing}))(ReduxRouterConnect); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment