Topics:
- Single Page Applications
- Routing, declarative component based routing
- Browser vs Hash History
- 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.
- cd into the repository and run
yarn install - run
yarn start
- You'll notice we've already installed react-router-dom for you.
importyour Router as BrowserRouter 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 the
REACT DEV TOOLSexpand<BrowserRouter>and highlight<Router>and notice that here is a"history"object on props.
- 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 incluce the
exactprop on the root component for/to make sure that it's rendering the exact component and not all the other components. - You should now be able to type
/,/about,/contactafterlocalhost:PORT/to see what's a
- Inside of
Navigation.jsimport { Link } from 'react-router-dom' - Delete
NavLinkand replace it with<Link /> - Pro-tip: highlight NavLink and use
cmd/ctrl + dto select them all at once. - Change the href on
<Link>totoand specify the correct routes to navigate to. - Head over to your app and start navigating. You should be able to see your URLS changing their paths as you go.