Created
July 22, 2019 14:01
-
-
Save click2install/9102b1fd0f9a863757ae4ba38cfde2d9 to your computer and use it in GitHub Desktop.
React DebugRouter
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 React, { Component } from 'react'; | |
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; | |
import Login from 'components/Login' | |
import DefaultComponent from 'components/DefaultComponent' | |
class DebugRouter extends Router { | |
constructor(props){ | |
super(props); | |
console.log('initial history is: ', JSON.stringify(this.history, null,2)) | |
this.history.listen((location, action)=>{ | |
console.log( | |
`The current URL is ${location.pathname}${location.search}${location.hash}` | |
) | |
console.log(`The last navigation action was ${action}`, JSON.stringify(this.history, null,2)); | |
}); | |
} | |
} | |
class App extends Component { | |
render() { | |
return ( | |
<DebugRouter> | |
<Switch> | |
<Route exact path="/login" name="Login Page" component={Login} /> | |
<Route path="/" name="Home" component={DefaultComponent} /> | |
</Switch> | |
</DebugRouter> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment