liveBook · Manning

useState

As explained in React Hooks in Action: With Suspense and Concurrent Mode

useState is a React Hook that adds a state value and its state-updating function to a function component.

useState lets a function component own a value that persists across renders. React returns the current value and an updater; calling the updater changes state and prompts React to recalculate the UI. This keeps an interface such as a selectable booking list synchronized with user actions. section 2.0 section 2.2.2

Call the Hook with an optional initial value and commonly use array destructuring: const [value, setValue] = useState(initialValue). The names are your choice, although set is the conventional updater prefix. Direct variable assignment does not notify React. section 2.2.1 section 2.2.2

Use the updater according to the state you are changing:

  • Pass a replacement value for simple state: setBookableIndex(3). The new value replaces the old one, and React schedules a re-render when it changes. section 2.2.3 section 2.5
  • Pass a function when the next value depends on the previous one: setBookableIndex(i => (i + 1) % bookablesInGroup.length). React supplies the latest state. section 2.2.5
  • Treat object state as a replacement, not a merge. Copy unchanged properties before changing one: setState(state => ({ ...state, bookableIndex: index })). section 2.2.3
  • Call useState once for each independently managed value. React uses Hook call order to associate each value with its updater. section 2.3 section 2.3.1
  • Pass an initializer function for expensive initial-state computation: useState(() => initialState). React runs it only when it first calls the component. section 2.5

A selectable booking list

A BookablesList can track its group and selected item separately: const [group, setGroup] = useState("Kit"); const [bookableIndex, setBookableIndex] = useState(0); A select event calls setGroup(e.target.value), while a button calls setBookableIndex(i). React then re-renders the list and applies the selected styling from the current state. A Next button can safely advance with setBookableIndex(i => (i + 1) % bookablesInGroup.length). For shared state, a parent such as BookablesView can hold const [bookable, setBookable] = useState() and pass both value and updater to child components. section 2.3.1 section 2.2.5 section 6.4.1

Figure 2.1 When you change a value in a component, React should update the UI.

Follow the state-to-UI cycle: changing a tracked value invokes its updater, React re-renders the component, and the returned UI reflects the latest state. section 2.0

How React Hooks in Action: With Suspense and Concurrent Mode covers it

How deep it goes
Explains initialization, replacement, functional updates, object copying, multiple calls, and lifted state, without descending into React internals.
Examples
Uses BookablesList selection, group drop-downs, next-item navigation, counters, and parent-managed selected bookables.
What it assumes
Assumes familiarity with React function components and JavaScript arrays, functions, destructuring, and object spread.
What you can do after
Build stateful function components, update values from events, preserve prior object properties, and share state through props.
How it connects
Connects function components with React Hooks, re-rendering, object state, useReducer, useRef, and lifted state.

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 Hooks in Action: With Suspense and Concurrent Mode. The section links point to the parts of the book it draws on.