Skip to content

Instantly share code, notes, and snippets.

@esamattis
Last active March 26, 2019 01:48
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save esamattis/ddd916d568ed2c1b30a7c714effabd01 to your computer and use it in GitHub Desktop.
Save esamattis/ddd916d568ed2c1b30a7c714effabd01 to your computer and use it in GitHub Desktop.
// Usage
<Router>
<Provider store={store}>
<ListeningRouter>
<Main />
</ListeningRouter>
</Provider>
</Router>
// Workaround for React Router v4 blocked updates issue/feature
// https://github.com/ReactTraining/react-router/blob/7f002d35deae5d32dcf27eef9ae9296d27028ee4/packages/react-router/docs/guides/blocked-updates.md
// https://github.com/ReactTraining/react-router/issues/4671
import React from "react";
import {pick} from "lodash/fp";
import {connect} from "react-redux";
import {withRouter, Route as RRoute, Switch as RSwitch} from "react-router-dom";
const UPDATE_LOCATION = "UPDATE_LOCATION";
const updateLocation = location => ({type: UPDATE_LOCATION, location});
export class ListeningRouter extends React.Component {
componentWillMount() {
this.props.updateLocation(this.props.location);
}
componentWillReceiveProps(nextProps) {
this.props.updateLocation(nextProps.location);
}
render() {
return React.Children.only(this.props.children);
}
}
ListeningRouter = withRouter(connect(null, {updateLocation})(ListeningRouter));
const addLocation = connect(pick("location"));
export const Route = addLocation(RRoute);
export const Switch = addLocation(RSwitch);
export const routeReducer = (state, action) => {
if (action.type === UPDATE_LOCATION) {
return {...state, location: action.location};
}
return state;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment