Exercise: React Hooks
#
ObjectiveYour objective is to rewrite your App
and FilmsList
class components as functional components with hooks.
#
Getting StartedThis is part 4 of TrueCoders' React Exercise Series.
We'll be in the same react repository that you worked in for part 3.
#
StepsReact 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
FilmsList
from a class to a function - Remove the
render
method surround thereturn <ul>...</ul>
statement- we'll still be using the returned JSX, so be sure to keep that
- Specify
props
as the parameter to theFilmsList
function
#
Exercise 2: State Hook in FilmsList- Import and destructure
useState
from thereact
package - Remove the constructor
- Add a declaration for
list
andsetList
that will be destructured from an array that is returned from callinguseState([])
- Update the return statement to map over
list
instead ofthis.state.list
#
Exercise 3: Update getFilmsgetFilms
is currently a method. Add thefunction
keyword 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
films
data, no longer{ list: films }
- Change
#
Exercise 4: Effect Hook in FilmsList- Import and destructure
useEffect
from thereact
package - Remove the
componentDidMount
method - 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
App
from a class to a function - Remove the
render
method surround thereturn ...
statement- we'll still be using the returned JSX, so be sure to keep that
- Specify
props
as the parameter to theApp
function
#
Exercise 6: State Hook in App- Import and destructure
useState
from thereact
package - Remove the constructor
- Add a declaration for
list
andsetList
that will be destructured from an array that is returned from callinguseState(["ready", "set", "GO"])
- Add a declaration for
text
andsetText
that will be destructured from an array that is returned from callinguseState("")
- Update the return statement to map over
list
instead ofthis.state.list
- Update the return statement's
input
value to betext
instead ofthis.state.text
- Update the return statement's
input
onChange to callsetText
instead ofthis.setState
#
Exercise 7: Update onSubmitonSubmit
is currently a method. Add thefunction
keyword to make it a function value within this component scope- Update the function to:
- use
list
instead ofthis.state.list
- use
text
instead ofthis.state.text
- use
setList
instead ofthis.setState
- use
- Update the return statement's
form
onSubmit to beonSubmit
instead ofthis.onSubmit
#
Helpful LinksIf 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