-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Learn React with TypeScript
By :

We have already learned about the useState
Hook in previous chapters, but we will look at it again here and compare it against another state Hook we haven’t covered yet, useReducer
. We will expand the PersonScore
component we created in the last section to explore these state Hooks.
As a reminder, the useState
Hook allows state to be defined in a variable. The syntax for useState
is as follows:
const [state, setState] = useState(initialState);
We will enhance the PersonScore
component we created in the last section to store the person’s name in state
. We will also have state
for a score that is incremented, decremented, and reset using some buttons in the component. We will also add a loading
state to the component, showing a loading indicator when true
.
Carry out the following steps:
PersonScore.tsx
and add useState
to the React import
statement:import { useEffect, useState } from ‘react’;