Exercise: React Hooks
Objective#
Your objective is to rewrite your App and FilmsList class components as functional components with hooks.
Getting Started#
This is part 4 of TrueCoders' React Exercise Series.
We'll be in the same react repository that you worked in for part 3.
Steps#
React Series Part 4 has multiple exercises
- Exercise 1: FilmsList from Class to Function
- Exercise 2: State Hook in FilmsList
- Exercise 3: Update getFilms
- Exercise 4: Effect Hook in FilmsList
- Exercise 5: App from Class to Function
- Exercise 6: State Hook in App
- Exercise 7: Update onSubmit
Exercise 1: FilmsList from Class to Function#
- Change the
FilmsListfrom a class to a function - Remove the
rendermethod surround thereturn <ul>...</ul>statement- we'll still be using the returned JSX, so be sure to keep that
- Specify
propsas the parameter to theFilmsListfunction
Exercise 2: State Hook in FilmsList#
- Import and destructure
useStatefrom thereactpackage - Remove the constructor
- Add a declaration for
listandsetListthat will be destructured from an array that is returned from callinguseState([]) - Update the return statement to map over
listinstead ofthis.state.list
Exercise 3: Update getFilms#
getFilmsis currently a method. Add thefunctionkeyword to make it a function value within this component scope- Update the promise consumer that updates the list state
- Change
this.setState()tosetList - Be sure to only pass in the
filmsdata, no longer{ list: films }
- Change
Exercise 4: Effect Hook in FilmsList#
- Import and destructure
useEffectfrom thereactpackage - Remove the
componentDidMountmethod - Call
useEffect- first, pass in a callback function that will call
getFilms - second, pass in an empty dependency array
- first, pass in a callback function that will call
Exercise 5: App from Class to Function#
- Change the
Appfrom a class to a function - Remove the
rendermethod surround thereturn ...statement- we'll still be using the returned JSX, so be sure to keep that
- Specify
propsas the parameter to theAppfunction
Exercise 6: State Hook in App#
- Import and destructure
useStatefrom thereactpackage - Remove the constructor
- Add a declaration for
listandsetListthat will be destructured from an array that is returned from callinguseState(["ready", "set", "GO"]) - Add a declaration for
textandsetTextthat will be destructured from an array that is returned from callinguseState("") - Update the return statement to map over
listinstead ofthis.state.list - Update the return statement's
inputvalue to betextinstead ofthis.state.text - Update the return statement's
inputonChange to callsetTextinstead ofthis.setState
Exercise 7: Update onSubmit#
onSubmitis currently a method. Add thefunctionkeyword to make it a function value within this component scope- Update the function to:
- use
listinstead ofthis.state.list - use
textinstead ofthis.state.text - use
setListinstead ofthis.setState
- use
- Update the return statement's
formonSubmit to beonSubmitinstead ofthis.onSubmit
Helpful Links#
If you feel stuck, or would like to see the finished code for this exercise to check your work, check out:
- React #4: React Hooks Exercise Video on Vimeo
- React #4: React Hooks Exercise Repo on Github