Quick Start
You’ll need a React web app to add react-router
.
If you need to create one, the easiest way to get started is with a popular tool called Create React App.
First install create-react-app
, if you don’t already have it, and then make a new project with it.
npm install -g create-react-app
create-react-app demo-app
cd demo-app
Installation
React Router DOM is published to npm so you can install it with either npm
or yarn
.
npm install react-router-dom
Copy/paste either of the examples (below) into your src/App.js
.
Example: Basic Routing
In this example we have 3 ‘Page’ Components handled by the <Router>
.
Note: Instead of <a href="/">
we use <Link to="/">
.
import React from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
const Index = () => <h2>Home</h2>;
const About = () => <h2>About</h2>;
const Users = () => <h2>Users</h2>;
const AppRouter = () => (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about/">About</Link>
</li>
<li>
<Link to="/users/">Users</Link>
</li>
</ul>
</nav>
<Route path="/" exact component={Index} />
<Route path="/about/" component={About} />
<Route path="/users/" component={Users} />
</div>
</Router>
);
export default AppRouter;
Example: Nested Routing
This example shows how nested routing works. The route /topics
loads the Topics
component, which renders any further <Route>
's conditionally on the paths :id
value.
import React from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
const App = () => (
<Router>
<div>
<Header />
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/topics" component={Topics} />
</div>
</Router>
);
const Home = () => <h2>Home</h2>;
const About = () => <h2>About</h2>;
const Topic = ({ match }) => <h3>Requested Param: {match.params.id}</h3>;
const Topics = ({ match }) => (
<div>
<h2>Topics</h2>
<ul>
<li>
<Link to={`${match.url}/components`}>Components</Link>
</li>
<li>
<Link to={`${match.url}/props-v-state`}>Props v. State</Link>
</li>
</ul>
<Route path={`${match.path}/:id`} component={Topic} />
<Route
exact
path={match.path}
render={() => <h3>Please select a topic.</h3>}
/>
</div>
);
const Header = () => (
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/topics">Topics</Link>
</li>
</ul>
);
export default App;
Now you’re ready to tinker. Happy routing!