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

Learn React with TypeScript
By :

React component state
is a special variable that may change over the lifecycle of a component. In this section, we’ll learn about the state
variable and use it within our Alert
component. We will use state
to allow the alert to be closed by the user.
There isn’t a predefined list of states; we define what’s appropriate for a given component. Some components won’t even need any state – the Alert
component hasn’t required a state for the requirements so far.
The state is a key part of making a component interactive. When a user interacts with a component, the component’s output may need to change. A change to the state causes the component to refresh, more often referred to as re-render.
The state is defined using a useState
function from React. The useState
function is one of React’s Hooks. There is a whole chapter on React Hooks in Chapter 3, Using React Hooks.
The syntax for useState...