🏗️ Work in Progress...
This lesson will teach you:
- Converting stateful class components to function components using hooks
The goal for this lesson is to get experience working with React Class components and how to get the same functionality from a class component and a functional component using hooks.
The React application that you will be using is a simple number guessing game. The game is currently written using a mix of class components for components that have state and stateless functional components.
When you are done the application should have all of the same functionality, but the class components should be refactored to be function components that use the useState
hook to manage their stateful data.
The first step is to fork the source repository in GitHub and then clone your forked repository to your local development system. There are instructions for forking a repository in GitHub here: https://docs.github.com/en/get-started/quickstart/fork-a-repo
Once you have it cloned locally make sure to replace any instance of yarn start
with yarn dev
or npm run dev
as Vite uses dev
for starting the development server.
If you are using npm, just replace
yarn
withnpm
run and execute the same commands:
npm install
npm run dev
In the current version of the application there is a mix of class components and stateless function components. Take some time to look over the structure of the application so that you understand how it is constructed.
The class components in the current version are NumberGuessingGame
and GuessControl
. These are the components that you will be converting to be function components with hooks. The existing function components don't need any changes.
If you want to attempt this on your own without a step by step walkthrough first then leave the section below collapsed.
Click the arrow to expand this section and see step by step instructions to convert to function components with hooks.
- Open the
GuessControl.js
file. - Rename the current
GuessControl
class toGuessControlOld
if you want to keep it a reference while converting the code. - Create a new function component called
GuessControl
that will take anonGuess
prop. - Copy the return value from the render function in the class component to be the return value in the new function component. Remove any references to
this.
since those will be replaced with new references to local variables or props passed in to the function component. - Create a new state variable named
currentGuess
with settersetCurrentGuess
and default value of an empty string. Set thevalue
property for the input element to refer to this state value. (Make sure to importuseState
) - Create a
handleInputChange
function within the component that updates thecurrentGuess
state value when the user changes the value in the input. Set theonChange
property for the input element to refer to this function. - Create a
onSubmitGuess
function that calls theonGuess
prop with thecurrentGuess
value converted to a number and also resets thecurrentGuess
to an empty string when it is called. Set theonClick
property on the button to refer to this function. - If you still have the old class version around as
GuessControlOld
, delete it.
- Open the
NumberGuessingGame.js
file. - Rename the current
NumberGuessingGame
class toNumberGuessingGameOld
if you want to keep it a reference while converting the code. - Create a new function component called
NumberGuessingGame
. - Copy the logic and return value from the render function in the class component to be in the new function component. Remove any references to
this.
since those will be replaced with new references. - Create 3 state variables and their setters for
numberToGuess
,numberOfGuesses
, andlatestGuess
and initialize them to the same values from the class component version. (Make sure to importuseState
) - Create a
handleGuess
function that will be passed in to theGuessControl
component as theonGuess
prop. This function should take the guess as an argument and set thelatestGuess
state with the guess (converted to a number using the Number function) and increment thenumberOfGuesses
state. - Create a
handleReset
function within the component that resets all 3 of the state properties the same way the handleReset function from the class component reset them. Pass this function to theGameOver
component as theonReset
prop. - Update all references from the class component that referred to
this.<something>
to refer to the correct variable or function for the new function component. - If you still have the old class version around as
NumberGuessingGameOld
, delete it.