Exercise: React Router
#
ObjectiveYour objective is to add home and film routes to your react project. Additional goals will be to add filter functionality to the films page to filter by director.
#
Getting StartedThis is part 5 of TrueCoders' React Exercise Series.
We'll be in the same react repository that you worked in for part 4.
#
StepsReact Series Part 5 has two parts with multiple exercises each.
- Part 1: Setting up React Router with two pages, Home and Films.
- Part 2: Adding filter functionality to the Films list for director, and showing films stats based on the filtered list of films
#
Part 1This section covers setting up React Router with two pages, Home and Films.
#
Exercise 1: Restructure Project FilesTo clear space for React Router to be in App.jsx
, move your existing code from there to a specific page component.
- Create a new folder in
src/
calledpages/
- Within
pages/
, create a new file calledhome.page.jsx
- Copy the contents from
App.jsx
intohome.page.jsx
- Change the component name from
App
toHomePage
- Remove the
FilmsList
component from theHomePage
return statement (it will be it's own page component later 😉)
Your HomePage
component should resemble:
#
Exercise 2: Create a Films PageConvert FilmsList
to a page component.
- Create a new file in
pages/
calledfilms.page.jsx
- Copy the contents of
components/FilmsList.jsx
intofilms.page.jsx
- Rename
FilmsList
toFilmsPage
- Add to the return statement
- wrap the
ul
in adiv
- add an
h1
element that says "Studio Ghibli Films"
- wrap the
Your FilmsPage
should resemble:
#
Exercise 3: Export PagesAs a pattern, you may see index.js
files used as single entry points for module exports. Although not required, that is what you will follow moving forward.
- Create an
index.js
file inpages/
- Import
HomePage
andFilmsPage
- Export an object that contains both
HomePage
andFilmsPage
by default
Your index.js
file should resemble:
#
Exercise 4: Setup React RouterSetup React Router in App.jsx
.
- Use npm to install
react-router-dom
(from your terminal) - In
App.jsx
, importBrowserRouter
,NavLink
,Routes
andRoute
fromreact-router-dom
- Import
HomePage
andFilmsPage
from theindex.js
file inpages/
- Clear the current contents of
App
- Add a return statement that:
- returns
BrowserRouter
- with
Routes
rendered as a child ofBrowserRouter
- with two
Route
componentsHomePage
should be rendered for"/"
FilmsPage
should be rendered for"films"
- returns
- Add a
nav
inside theBrowserRouter
above theRoutes
- with a
ul
of twoli
- each
li
should contain aNavLink
- match one
NavLink
'sto
prop to"/"
, and give it text that displays "Home" - match the other
NavLink
'sto
prop to"films"
, and give it text that displays "Films"
- with a
#
Part 2This section covers adding filter functionality to the Films list by director.
#
Exercise 1: Setup Filter ElementsOpen films.page.jsx
.
- Declare another piece of state,
searchDirector
andsetSearchDirector
, that will be destructured from the return ofuseState("")
- Add a
form
to the return statement beneath the existingh1
- Add a
div
with class nameform-group
inside of theform
- Add a
label
andselect
inside of thediv.form-group
- set the
select
'svalue
prop to thesearchDirector
state - set the
select
'sonChange
prop to a function that callssetSearchDirector
and updatessearchDirector
with the choseoption
value - add a single
option
to theselect
(for now) with thevalue
set to""
and text content displaying"All"
- set the
#
Exercise 2: Helper Functions for Film DirectorsCreate some helper functions that can be used with the Studio Ghibli film data.
- Create a new folder in
src/
calledhelpers/
- Create a file in
helpers/
calledfilm.helpers.js
- In
film.helpers.js
, create and export a function calledfilterFilmsByDirector
- In
film.helpers.js
, create and export a function calledgetListOf
filterFilmsByDirector
#
Exercise 3: The goal of filterFilmsByDirector
, as per the name, is to receive list
(array) and director
(string) parameters, and return a filtered list of films where only the films by a the specified director are included.
For example:
- Implement
filterFilmsByDirector
- Once done, import
filterFilmsByDirector
infilms.page.jsx
- Call
filterFilmsByDirector
before your return statement- pass in
list
(state) andsearchDirector
(state) as parameters - assign the result to a variable called
filmsByDirector
- pass in
- In your return statement, change
list.map(...)
tofilmsByDirector.map(...)
getListOf
#
Exercise 4: The goal of getListOf
is to receive list
(array) and prop
(string) parameters, and return a cumulative list of items including every unique value that exists in the list at the specified property.
For example:
- Implement
getListOf
- Once done, import
getListOf
infilms.page.jsx
- Call
getListOf
before your return statement- pass in
list
(state) and"director"
as parameters - assign the result to a variable called
directors
- pass in
- In your return statement, within your
select
and below the<option value="">All</option>
- use the
map
array method to return a new array ofoption
elements, one per item indirectors
- the
value
prop and text content should both be set to the director
- use the
#
Helpful LinksIf you feel stuck, or would like to see the finished code for this exercise to check your work, check out:
- React #5: React Router Exercise Video on Vimeo
- React #5: React Router Exercise Repo on Github