Exercise: React Router Hooks
#
ObjectiveYour objective is to continue from the React Routing Exercise to create an individual page view for each film. Additional goals will be to display film stats based on the current filter applied to our list of films.
#
Getting StartedThis is part 6 of TrueCoders' React Exercise Series.
You'll be in the same react repository that you worked in for part 5.
#
StepsReact Series Part 6 has two parts with multiple exercises each.
#
Part 1#
Exercise 1: Creating SingleFilm PageStart off by creating the component for our film page.
- Create a new file in
pages/
calledsinglefilm.page.jsx
- Create a functional component called
SingleFilmPage
insinglefilm.page.jsx
- make sure to export it (I tend to forget unless I do it early on)
- Import
useState
from thereact
package - Import
useParams
from thereact-router-dom
package - Declare a piece of state,
item
andsetItem
, that will be destructured from the return ofuseState({})
- Call
useParams
- Destructure
id
from the return object
- Destructure
- Add the following to the return statement
Feel free to change anything about this return statement from content to style. We'll use this to display information about a single film.
#
Exercise 2: getFilmSimilar to our getFilms
function in films.page.jsx
, create a function to get a single Studio Ghibli film.
- In
SingleFilmPage
, create a function calledgetFilm
- The function should call the
fetch
function with the following url parameter: https://ghibliapi.herokuapp.com/films/${id}id
in this case will need to be interpolated since we'll be getting that value dynamically from the route params
- Call the
then
method on the returned promise- the first
then
call should receive a callback function that returns the result parsed to json
- the first
- Make another
then
call on the returned promise- the second
then
call should receive a callback function that usessetItem
to setitem
equal to the result
- the second
- Lastly add a
catch
method call that should receive a callback function that will handle an error if one occurs
#
Exercise 3: Calling getFilm in useEffectCall getFilm
when the component mounts.
- Import
useEffect
from thereact
package - Call
useEffect
with- a callback function passed in that calls
getFilm
- an empty dependency array passed in
- a callback function passed in that calls
#
Exercise 4: Links to Single FilmsAdd links to a single page view.
- In
pages/films.page.jsx
, importLink
fromreact-router-dom
- Update the
li
's rendered bylist.map(...)
to include aLink
- wrap the
item.title
in aLink
that has apath
set tofilm/${film.id}
- make sure the path is interpolated to include the film's
id
as a url parameter
- wrap the
#
Exercise 5: A Route for SingleFilmPageLastly, set up a route for SingleFilmPage
.
- In
pages/index.js
, importSingleFilmPage
and export it withHomePage
andFilmsPage
- In
App.jsx
, addSingleFilmPage
to the destructured imports frompages/
- Add a
Route
withinRoutes
that rendersSingleFilmPage
for"film/:id"
paths
#
Part 2getFilmStats
#
Exercise 1: Create Create and export a new helper function in film.helpers.js
called getFilmStats
.
The goal of getFilmStats
is to receive list
(array) parameter, and return a object that contains the following:
avg_score
as a number, being the averagert_score
of the list of filmsacc_score
as a number, being the sumrt_score
of the list of filmstotal
as a number, being the amount of films in the listlatest
as a number, being the latest release year for a film in the list
For example:
- Implement
getFilmStats
#
Exercise 2: Display Film StatsCreate several elements for displaying average film score, latest film and total films for the filtered list of films.
- In
films.page.jsx
, importgetFilmStats
fromfilm.helpers.js
- Call
getFilmStats
before the return statement and passlist
as a parameter- destructure
avg_score
,total
andlatest
from the result
- destructure
- Add the following code to the return statement between the
form
andul
#
Helpful LinksIf you feel stuck, or would like to see the finished code for this exercise to check your work, check out: