Listing 12-1: A Simple Routing Component
You come to a fork in the road. Which path will you take?
import React from "react";
import {LessTraveledPath,MoreTraveledPath} from './PathOptions';
import {
BrowserRouter,
Switch,
Route,
Link
} from "react-router-dom";
function ChooseYourAdventure() {
return (
<BrowserRouter>
<div>
<p>You come to a fork in the road. Which path will you take?</p>
<ul>
<li>
<Link to="/worn">The More Well-traveled Path</Link>
</li>
<li>
<Link to="/untrodden">The Less Well-traveled Path</Link>
</li>
</ul>
<Switch>
<Route path="/worn">
<MoreTraveledPath />
</Route>
<Route path="/untrodden">
<LessTraveledPath />
</Route>
</Switch>
</div>
</BrowserRouter>
);
}
export default ChooseYourAdventure;
Download the examples, report issues, and ask/answer questions in the discussion area by visiting the book's github page. All of the code for the book is also available on codesandbox.io for you to play around with.
ReactJS Foundations is published by John Wiley and Sons, Inc and is available in paperback and eBook.