Exercise: React Router
Objective#
Your 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 Started#
This is part 5 of TrueCoders' React Exercise Series.
We'll be in the same react repository that you worked in for part 4.
Steps#
React 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 1#
This section covers setting up React Router with two pages, Home and Films.
Exercise 1: Restructure Project Files#
To 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.jsxintohome.page.jsx - Change the component name from
ApptoHomePage - Remove the
FilmsListcomponent from theHomePagereturn statement (it will be it's own page component later 😉)
Your HomePage component should resemble:
Exercise 2: Create a Films Page#
Convert FilmsList to a page component.
- Create a new file in
pages/calledfilms.page.jsx - Copy the contents of
components/FilmsList.jsxintofilms.page.jsx - Rename
FilmsListtoFilmsPage - Add to the return statement
- wrap the
ulin adiv - add an
h1element that says "Studio Ghibli Films"
- wrap the
Your FilmsPage should resemble:
Exercise 3: Export Pages#
As 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.jsfile inpages/ - Import
HomePageandFilmsPage - Export an object that contains both
HomePageandFilmsPageby default
Your index.js file should resemble:
Exercise 4: Setup React Router#
Setup React Router in App.jsx.
- Use npm to install
react-router-dom(from your terminal) - In
App.jsx, importBrowserRouter,NavLink,RoutesandRoutefromreact-router-dom - Import
HomePageandFilmsPagefrom theindex.jsfile inpages/ - Clear the current contents of
App - Add a return statement that:
- returns
BrowserRouter - with
Routesrendered as a child ofBrowserRouter - with two
RoutecomponentsHomePageshould be rendered for"/"FilmsPageshould be rendered for"films"
- returns
- Add a
navinside theBrowserRouterabove theRoutes- with a
ulof twoli - each
lishould contain aNavLink - match one
NavLink'stoprop to"/", and give it text that displays "Home" - match the other
NavLink'stoprop to"films", and give it text that displays "Films"
- with a
Part 2#
This section covers adding filter functionality to the Films list by director.
Exercise 1: Setup Filter Elements#
Open films.page.jsx.
- Declare another piece of state,
searchDirectorandsetSearchDirector, that will be destructured from the return ofuseState("") - Add a
formto the return statement beneath the existingh1 - Add a
divwith class nameform-groupinside of theform - Add a
labelandselectinside of thediv.form-group- set the
select'svalueprop to thesearchDirectorstate - set the
select'sonChangeprop to a function that callssetSearchDirectorand updatessearchDirectorwith the choseoptionvalue - add a single
optionto theselect(for now) with thevalueset to""and text content displaying"All"
- set the
Exercise 2: Helper Functions for Film Directors#
Create 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
Exercise 3: filterFilmsByDirector#
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
filterFilmsByDirectorinfilms.page.jsx - Call
filterFilmsByDirectorbefore 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(...)
Exercise 4: getListOf#
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
getListOfinfilms.page.jsx - Call
getListOfbefore 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
selectand below the<option value="">All</option>- use the
maparray method to return a new array ofoptionelements, one per item indirectors - the
valueprop and text content should both be set to the director
- use the
Helpful Links#
If 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