Topics:
- Single Page Applications
- Routing, declaritive component based routing
- Browser vs Hash History
- Path and Component
- cd into repo and run
npm install - run
npm start
- You'll notice we've already installed react-router-dom for you.
- Step 1:
import { BrowserRouter as Router } from 'react-router-dom';inside yourindex.jsfile. - Step 2: Wrap your
<App />component that you're passing toReactDOM.render()with your<Router> </Router>component. - Step 3: open up your chrome
REACT DEV TOOLSand notice your app is now all wrapped inBrowserRouter - Step 4: inside the
REACT DEV TOOLSexpand<BrowserRouter>and highlight<Router>and notice that here is a"history"object on props.
- Step 5: inside of your
App.jsfileimport { Route } from 'react-router-dom';- this is where we're going to declare and specify our router.
- Step 6: 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.
- be sure to incluce the
- Step 7: you should now be able to type
/,/about,/contactafterlocalhost:PORT/to see what's a
- Step 8: inside of
Navigation.jsimport { Link } from 'react-router-dom' - Step 9: delete
NavLinkand replace it with<Link />- Pro-tip: highlight NavLink and use
cmd/ctrl + dto select them all at once.
- Pro-tip: highlight NavLink and use
- Step 10: change the href on
<Link>totoand specify the correct routes to navigate to. - Step 11: head over to your app and start navigating. You should be able to see your URLS changing their paths as you go.