Created
April 17, 2020 11:36
-
-
Save simonespa/911f8c54c4c864127ba0ed7bc5edbe78 to your computer and use it in GitHub Desktop.
React Router DOM - Page Switcher Listener
This file contains hidden or 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, { 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