Skip to content

Instantly share code, notes, and snippets.

@jackcallister
Created September 6, 2015 17:13
Show Gist options
  • Save jackcallister/5517e5943632b162c206 to your computer and use it in GitHub Desktop.
Save jackcallister/5517e5943632b162c206 to your computer and use it in GitHub Desktop.
Redux Router Reducer
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);
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')
);
});
import {
UPDATE_ROUTER_STATE,
} from '../constants/RouterConstants';
export function updateRouterState(payload) {
return {
type: UPDATE_ROUTER_STATE,
payload: payload
}
}
export const UPDATE_ROUTER_STATE = 'UPDATE_ROUTER_STATE';
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