liveBook · Manning

useState

As explained in React in Depth

useState is a React hook that adds a state value and its updater function to a function component.

useState gives a function component a state value and a setter. In TypeScript, the key decision is whether the initializer describes every value that state may hold later. An initial boolean or string usually gives useful inference; a placeholder such as null or an empty array may not. section 6.1.1

Type an undecided response

An appointment response can start undecided, then become accepted or rejected. `const [isAccepted, setIsAccepted] = useState<null | boolean>(null)` permits all three states. Without the type argument, TypeScript infers `null`, so `setIsAccepted(true)` fails. A separate message can use `const [message, setMessage] = useState("")`; submission can wait until `typeof isAccepted === "boolean"`. section 6.1.1

Choose the type from the intended updates, not just the starting value:

  • For tags, `useState([])` infers `never[]`, which rejects adding a string. Use `useState<string[]>([])`, then an update such as `setTags((oldTags) => [...oldTags, newTag])`. section 6.1.1
  • For a visibility lookup keyed by user, initialize with `useState<Record<string, boolean>>({})`. section 6.1.1

Local state can also underpin shared application data. A goal tracker uses one useState call for its list and another for the current item, with functions wrapping their setters and context distributing the data. A version built with useState alone works, but is not treated as the ideal endpoint; useReducer, Redux Toolkit, Zustand, and XState are other approaches considered for the same application. The choice depends on the data-management needs rather than a rule that every state value needs another library. section 8.3 section 8.1

For debugging, React Developer Tools’ components inspector can display and change a useState value directly; the component re-renders without calling its usual setter. Context is different: a consumer’s context value can be inspected there, but changes must be made at the provider. section 4.4.3

Figure 4.14 When you’re inspecting a stateful component with a useState or useReducer hook, you can interact with the state directly through the components inspector. You can manipulate the value, just as you can manipulate properties, as shown in figure 4.12.
figure

The inspector view shows where a hook state value can be changed directly while examining a stateful component. section 4.4.3

How React in Depth covers it

How deep it goes
Works through initializer inference, union and collection types, setter errors, and application-level placement, without detailing hook internals.
Examples
Uses an appointment response, a tag form, a goal tracker, and a dark-mode inspector session.
What it assumes
Assumes familiarity with React state and setters while addressing TypeScript-specific inference problems.
What you can do after
Apply it via React useState with explicit TypeScript types when initial values omit valid future states.
How it connects
Connects useState to TypeScript inference, React context, useReducer, Redux Toolkit, Zustand, and XState.

Also explained in

Compare how 3 books explain useState →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React in Depth. The section links point to the parts of the book it draws on.