Topics:
- Single Page Applications
- React Router V5 (react-router-dom)
- Routing, declarative component based routing
- Path and Component
- A starter pack project for all things React Router. Learn to set up a basic routing system within an application. Use this application to help guide your learning over the next couple of days.
- Note that this project has already been boilerplated for you. There is no need to run any installation steps beyond
npm install.
- cd into the repository and run
npm install - run
npm start
- You'll notice we've already installed
react-router-domfor you. -
importyour BrowserRouter as Router inside yourindex.jsfile. - Wrap your
<App />component that you're passing toReactDOM.render()with your newRoutercomponent. - open up your chrome
REACT DEV TOOLSand notice your app is now all wrapped inBrowserRouter
- Inside of your
App.jsfileimport { Route } from 'react-router-dom'; - This is where we're going to declare and specify our router.
- Create 3
<Route />setting theirpathprop equal to/,/about,/contactwith their respective components. - Be sure to include the
exactprop on the root component for/to make sure that it's rendering the exact component and not all the other components.
- Inside of
Navigation.jsimport { Link } from 'react-router-dom'. - Declare the
toas the href on<Link>and specify the correct routes for your app to navigate towards. - Head over to your app and start navigating. You should be able to see your URLS changing their paths as you go. Each path should display the proper component.