Created
January 11, 2018 19:42
-
-
Save Sstobo/2127d1f347ecb94480e9bf3dc74b62f2 to your computer and use it in GitHub Desktop.
[React Routing] #react #routing
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, { Component } from 'react'; | |
import { render } from 'react-dom'; | |
import { | |
BrowserRouter as Router, | |
Route, | |
Switch, | |
Link, | |
Redirect | |
} from 'react-router-dom'; | |
import Hello from './Components/Hello'; | |
import Goodbye from './Components/Goodbye'; | |
import './style.css'; | |
class App extends Component { | |
render() { | |
return ( | |
<div> | |
<Router> | |
<div> | |
<Link to="/hello">Hello</Link>{" --- "} | |
<Link to="/goodbye">GoodBye</Link>{"-----"} | |
<Link to="/">Home</Link> | |
<Switch> | |
<Route exact path="/" component= {() => { | |
return <h1> Home! </h1> | |
}} /> | |
// paramaterized route | |
<Route exact path="/hello/:name" component={Hello} /> | |
<Route exact path="/goodbye" component={Goodbye} /> | |
</Switch> | |
</div> | |
</Router> | |
</div> | |
); | |
} | |
} | |
render(<App />, document.getElementById('root')); | |
###### in Hello.js | |
###### takes the parameter | |
({match}) => <p> Hello {match.params.name}!</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import { withRouter} from 'react-router-dom';
const Hello = ({ match, history )} => (
);<button onClick={() => history.push("/"} > Go Home {match.params.name}
export default withRouter(Hello);