Skip to content

Instantly share code, notes, and snippets.

@Sstobo
Created January 11, 2018 19:42
Show Gist options
  • Save Sstobo/2127d1f347ecb94480e9bf3dc74b62f2 to your computer and use it in GitHub Desktop.
Save Sstobo/2127d1f347ecb94480e9bf3dc74b62f2 to your computer and use it in GitHub Desktop.
[React Routing] #react #routing
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>
@Sstobo
Copy link
Author

Sstobo commented Jan 11, 2018

import { withRouter} from 'react-router-dom';

const Hello = ({ match, history )} => (


<button onClick={() => history.push("/"} > Go Home {match.params.name}

);

export default withRouter(Hello);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment