Walkthrough: Routing
List of tasks
-
Add
react-router-dom
to your project's dependencies. -
Wrap the
<BrowserRouter />
component around your<App />
component. -
Create a
<Properties />
component. For now, it should be a presentational component that renders a<div>
with the contents "Properties Page". -
Create a
<AddProperty />
component. For now, it should be a presentational component that renders a<div>
with the contents "Add Property Page". -
Inside your
<App />
component, underneath the<NavBar />
, render a<Routes />
component. -
Inside the
<Routes />
statement, add a new<Route />
component. It should have apath
prop set to/
(for when the user is on the homepage). It should render the<Properties />
component - see element. -
Add another
<Route />
component for when the user visits the pathadd-property
. This time it should render the<AddProperty />
component. -
Inside the
<NavBar />
component, import in the<Link />
component. -
Swap your
<a>
elements for<Link />
elements. Add ato
prop to the<Link />
element wrapping "View Properties" and set it's value to/
. Set your other<Link />
'sto
prop toadd-property
. -
Everything should still look okay in your browser now. You might need to look at changing the text color and text-decoration in the
.navbar-links-item
CSS class to get the appearance you want. -
Give the links a go. If everything has been done correctly then they should alternate between your
<Properties />
and<AddProperty />
components without reloading the page.
1 - Add react-router-dom
to your project's dependencies.
Do this without the walkthrough. 💪
Hint: use npm install react-router-dom
- Wrap the
<BrowserRouter />
component around your<App />
component.
The <BrowserRouter />
component instructs React Router to use HTML5's History API to handle navigation. We wrap the outermost component, also known as the entry point of our application, so that routing is available in all application components.
In src/index.js
, destructure BrowserRouter
from the react-router-dom
package:
And wrap the <App />
component passed to root.render()
method inside the <BrowserRouter />
component:
3 - Create a <Properties />
component.
For now, it should be a presentational component that renders a <div>
or Fragment with the contents "Properties Page".
You could have also used a React <Fragment />
here. Think what typical React problem using <Fragment />
can solve? If you aren't familiar with Fragment
it essentially wraps our JSX children in the same way a div
does, but without rendering a pointless empty div to the DOM. It can be represented as <Fragment></Fragment>
or by the shorthand <></>
4 - Create a <AddProperty/>
component.
For now, it should be a presentational component that renders a <div>
with the contents "Add Property Page".
Do this without the walkthrough. 💪
5 - Inside your <App />
component, underneath the <NavBar />
, render a <Routes />
component.
Don't forget to import Routes
from react-router-dom
.
The <Routes />
component houses all of our routes, its purpose is to match the given child routes to the current location.
6 - Inside the <Routes />
statement, add a new <Route />
component.
Don't forget to import Route
from react-router-dom
.
The <Route />
component should have two props:
path
set to/
,element
should point to the<Properties />
component .
You should have a linter error here on Properties
. We'll leave you to fill in the missing puzzle piece.
7 - Add another <Route />
component for when the user visits the path /add-property
.
This time it should render the <AddProperty />
component.
Do this without the walkthrough. 💪
8 - Inside the <NavBar />
component, import in the <Link />
component from react-router-dom
.
There is a code example of how to do this on the <Link />
documentation.
9 - Swap your <a>
elements for <Link />
elements.
Add a to
prop to the <Link />
element wrapping "View Properties" and set it's value to /
. Set your other <Link />
's to
prop to add-property
.
Example:
10 - Try it out in the browser
If everything has been done correctly then they should alternate between your <Properties />
and <AddProperty />
components without seeing the page reload. You should see text underneath the <NavBar/>
changing from "Properties Page" to "Add Property Page".
Do take time here to tinker with your CSS and ensure everything looks aesthetically pleasing.