liveBook · Manning

Initialization argument

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

An initialization argument is the value passed to useReducer for an initialization function to use when computing initial state.

An initialization argument is the second argument passed to useReducer. When an initialization function is supplied as the third argument, React passes this value to that function to compute the reducer’s initial state. section 3.3

Initializing a WeekPicker

A date-based WeekPicker can derive its initial state from an initialization argument: const [state, dispatch] = useReducer(reducer, initArgument, initFunction); On the first call, state is the initialization function’s return value; later calls use the current reducer state. section 3.3 section 3.5

Figure 3.6 The initialization function for useReducer uses the initialization argument to generate the reducer’s initial state.

Trace how the initialization function transforms the initialization argument into the reducer’s initial state. section 3.3

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

How deep it goes
Defines the argument’s role and first-call behavior, without detailing the initialization function’s internal implementation.
Examples
Uses the WeekPicker component and date-and-week utility work as the concrete application.
What it assumes
Assumes familiarity with reducers and useReducer while distinguishing initialization from subsequent dispatched updates.
What you can do after
Apply it via useReducer to derive initial state when initialization is expensive or uses an existing function.
How it connects
Connects initialization arguments with initialization functions, initial state, reducers, and useReducer.

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.