liveBook · Manning

State

As explained in Essential TypeScript 5, Third Edition

Component state is data maintained by a user-interface component whose changes can trigger a re-render.

State is data maintained by a React component for values that change during interaction and trigger updated content. A product item can keep a selected quantity as local state while receiving the product and cart callback through props. section 20.4

TypeScript React components manage state in two main ways:

  • Class components describe state with a State interface and initialize it through the component constructor. section 20.4
  • Class components apply changes with setState; React uses that method to recognize that an update occurred. section 20.4
  • Functional components use the useState hook, which returns a readable value and a separate updater function. section 20.4.1
  • Generic type arguments let TypeScript check state properties and values passed to state-update functions. section 20.4 section 20.4.1

Tracking a selected quantity

A functional product component can declare `const [quantity, setQuantity] = useState<number>(1)`. A select element converts its selected value to a number and calls `setQuantity`; expressions read the current `quantity` when invoking the cart callback. section 20.4.1

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains typed class state, setState, and useState with a quantity example, but does not descend into React lifecycle internals.
Examples
Uses a product item whose select control changes a numeric quantity before an Add To Cart callback runs.
What it assumes
Assumes familiarity with React components, JSX, props, and TypeScript interfaces while demonstrating state typing and updates.
What you can do after
Apply state via typed class components with setState or functional components with useState and an updater function.
How it connects
Connects state with React components, props, event handling, hooks, useState, setState, and TypeScript generics.

Also explained in

Compare how 39 books explain state →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.