1import React, { Component } from 'react';
2import { render } from 'react-dom';
3import { BrowserRouter, Route, Switch, Link } from 'react-router-dom';
4import { Home } from 'wherever-you-put-it/home.component.jsx';
5import { Something } from 'wherever-you-put-it/something.component.jsx';
6import { SomethingElse } from 'wherever-you-put-it/something-else.component.jsx';
7
8class App extends Component {
9 render() {
10 return (
11 <BrowserRouter>
12 <Switch>
13 <Route path="/">
14 <Home />
15 </Route>
16 <Route path="/something">
17 <Something />
18 </Route>
19 <Route path="/somethingelse">
20 <SomethingElse />
21 </Route>
22 </Switch>
23 <SomeComponentOrElement>
24 <Link to="/">Home</Link>
25 <Link to="/something">Something</Link>
26 <Link to="/somethingelse">Something Else</Link>
27 </SomeComponentOrElement>
28 </BrowserRouter>
29 )
30 }
31}
32
33render(<App />, document.getElementById('app'));
1import { Route, Redirect } from 'react-router'
2
3<Route exact path="/" render={() => (
4 loggedIn ? (
5 <Redirect to="/dashboard"/>
6 ) : (
7 <PublicHomePage/>
8 )
9)}/>
1<Route exact path="/">
2 {loggedIn ? <Redirect to="/profile" /> : <HomePage />}
3</Route>
1<Route exact path="/">
2 {loggedIn ? <Redirect to="/dashboard" /> : <PublicHomePage />}
3</Route>
1npm install react-router-dom
2import {BrowserRouter, Switch, Route, Link} from "react-router-dom"
3import React from "react";
4import {
5 BrowserRouter as Router,
6 Switch,
7 Route,
8 Link
9} from "react-router-dom";
10
11export default function App() {
12 return (
13 <Router>
14 <div>
15 <nav>
16 <ul>
17 <li>
18 <Link to="/">Home</Link>
19 </li>
20 <li>
21 <Link to="/about">About</Link>
22 </li>
23 <li>
24 <Link to="/users">Users</Link>
25 </li>
26 </ul>
27 </nav>
28
29 {/* A <Switch> looks through its children <Route>s and
30 renders the first one that matches the current URL. */}
31 <Switch>
32 <Route path="/about">
33 <About />
34 </Route>
35 <Route path="/users">
36 <Users />
37 </Route>
38 <Route path="/">
39 <Home />
40 </Route>
41 </Switch>
42 </div>
43 </Router>
44 );
45}
46
47function Home() {
48 return <h2>Home</h2>;
49}
50
51function About() {
52 return <h2>About</h2>;
53}
54
55function Users() {
56 return <h2>Users</h2>;
57}
58