Skip to content

Instantly share code, notes, and snippets.

@simonespa
Created April 17, 2020 11:36
Show Gist options
  • Select an option

  • Save simonespa/911f8c54c4c864127ba0ed7bc5edbe78 to your computer and use it in GitHub Desktop.

Select an option

Save simonespa/911f8c54c4c864127ba0ed7bc5edbe78 to your computer and use it in GitHub Desktop.
React Router DOM - Page Switcher Listener
import React, { memo } from 'react';
import PropTypes from 'prop-types';
import { Switch, Route, withRouter } from 'react-router-dom';
import HomePage from './HomePage';
import NotFoundPage from './NotFoundPage';
class PageSwitcher extends React.PureComponent {
static propTypes = {
location: PropTypes.object.isRequired
}
componentDidUpdate(prevProps) {
if (this.props.location !== prevProps.location) {
this.onRouteChanged();
}
}
onRouteChanged() {
console.log("ROUTE CHANGED");
}
render(){
return (
<Switch>
<Route exact path="/" component={HomePage} />
<Route component={NotFoundPage} />
</Switch>
);
}
}
PageSwitcher.displayName = 'PageSwitcher';
export default withRouter(PageSwitcher);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment