Created
September 6, 2015 17:13
-
-
Save jackcallister/5517e5943632b162c206 to your computer and use it in GitHub Desktop.
Redux Router Reducer
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 { | |
updateRouterState | |
} from '../actions/RouterActions'; | |
function mapStateToProps(store) { | |
return {} | |
} | |
function mapDispatchToProps(dispatch) { | |
return { | |
updateRouterState: (state) => dispatch(updateRouterState(state)) | |
} | |
} | |
class App extends React.Component { | |
componentWillReceiveProps(nextProps) { | |
this.props.updateRouterState(nextProps.router); | |
} | |
render() { | |
return ( | |
<main> | |
<RouteHandler /> | |
</main> | |
); | |
} | |
} | |
export default connect(mapStateToProps, mapDispatchToProps)(App); |
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
Router.run(routes, Router.HistoryLocation, (Handler, state) => { | |
const router = { | |
path: state.path, | |
pathname: state.pathname, | |
params: state.params, | |
query: state.query | |
} | |
React.render( | |
<Provider store={store}> | |
{() => <Handler router={router} />} | |
</Provider>, | |
document.getElementById('app') | |
); | |
}); | |
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 { | |
UPDATE_ROUTER_STATE, | |
} from '../constants/RouterConstants'; | |
export function updateRouterState(payload) { | |
return { | |
type: UPDATE_ROUTER_STATE, | |
payload: payload | |
} | |
} |
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 const UPDATE_ROUTER_STATE = 'UPDATE_ROUTER_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 { | |
UPDATE_ROUTER_STATE, | |
} from '../constants/RouterConstants'; | |
const initialState = { | |
path: null, | |
pathname: null, | |
params: {}, | |
query: {} | |
} | |
export default function router(state = initialState, action) { | |
switch(action.type){ | |
case UPDATE_ROUTER_STATE: | |
return action.payload; | |
default: | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment