Last active
August 25, 2020 16:45
-
-
Save karol-dabrowski/44357592d62dfbf58f72e895031a10ef to your computer and use it in GitHub Desktop.
Getting parameters from URL with React Router
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, Switch, Route } from 'react-router-dom'; | |
import User from './User'; | |
class App extends Component { | |
render() { | |
return ( | |
<Router> | |
<Switch> | |
<Route exact path='/user/:userName' component={User} /> | |
<Route> | |
<div>Default page</div> | |
</Route> | |
</Switch> | |
</Router> | |
); | |
} | |
} | |
export default App; |
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
// Functional component | |
import React from 'react'; | |
import { useParams } from 'react-router-dom'; | |
const User = () => { | |
const { userName } = useParams(); | |
return ( | |
<div>Username: { userName }</div> | |
); | |
} | |
export default User; |
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
// Class component | |
import React, { Component } from 'react'; | |
class User extends Component { | |
render() { | |
const { userName } = this.props.match.params; | |
return ( | |
<div>Username: { userName }</div> | |
); | |
} | |
} | |
export default User; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment