Exercise: React Router Hooks
Objective#
Your 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 Started#
This is part 6 of TrueCoders' React Exercise Series.
You'll be in the same react repository that you worked in for part 5.
Steps#
React Series Part 6 has two parts with multiple exercises each.
Part 1#
Exercise 1: Creating SingleFilm Page#
Start off by creating the component for our film page.
- Create a new file in
pages/calledsinglefilm.page.jsx - Create a functional component called
SingleFilmPageinsinglefilm.page.jsx- make sure to export it (I tend to forget unless I do it early on)
- Import
useStatefrom thereactpackage - Import
useParamsfrom thereact-router-dompackage - Declare a piece of state,
itemandsetItem, that will be destructured from the return ofuseState({}) - Call
useParams- Destructure
idfrom 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: getFilm#
Similar 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
fetchfunction with the following url parameter: https://ghibliapi.herokuapp.com/films/${id}idin this case will need to be interpolated since we'll be getting that value dynamically from the route params
- Call the
thenmethod on the returned promise- the first
thencall should receive a callback function that returns the result parsed to json
- the first
- Make another
thencall on the returned promise- the second
thencall should receive a callback function that usessetItemto setitemequal to the result
- the second
- Lastly add a
catchmethod call that should receive a callback function that will handle an error if one occurs
Exercise 3: Calling getFilm in useEffect#
Call getFilm when the component mounts.
- Import
useEffectfrom thereactpackage - Call
useEffectwith- 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 Films#
Add links to a single page view.
- In
pages/films.page.jsx, importLinkfromreact-router-dom - Update the
li's rendered bylist.map(...)to include aLink- wrap the
item.titlein aLinkthat has apathset tofilm/${film.id} - make sure the path is interpolated to include the film's
idas a url parameter
- wrap the
Exercise 5: A Route for SingleFilmPage#
Lastly, set up a route for SingleFilmPage.
- In
pages/index.js, importSingleFilmPageand export it withHomePageandFilmsPage - In
App.jsx, addSingleFilmPageto the destructured imports frompages/ - Add a
RoutewithinRoutesthat rendersSingleFilmPagefor"film/:id"paths
Part 2#
Exercise 1: Create getFilmStats#
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_scoreas a number, being the averagert_scoreof the list of filmsacc_scoreas a number, being the sumrt_scoreof the list of filmstotalas a number, being the amount of films in the listlatestas a number, being the latest release year for a film in the list
For example:
- Implement
getFilmStats
Exercise 2: Display Film Stats#
Create several elements for displaying average film score, latest film and total films for the filtered list of films.
- In
films.page.jsx, importgetFilmStatsfromfilm.helpers.js - Call
getFilmStatsbefore the return statement and passlistas a parameter- destructure
avg_score,totalandlatestfrom the result
- destructure
- Add the following code to the return statement between the
formandul
Helpful Links#
If you feel stuck, or would like to see the finished code for this exercise to check your work, check out: