React has become the dominant JavaScript library for front-end web development, largely through its useState construct that endows UI components with mutable state. Yet the mechanism underpinning useState—hook-indexed state—remains both fragile and poorly understood. Programmers frequently encounter unexpected behavior, and the benefits of programming in this paradigm are wholly unclear. Ueno, Cooper, Fowler, and Lindley address this gap by defining Lambda-React-Hook, a core calculus that captures the essence of hook-indexed state, and endowing it with a type system that statically rules out unexpected behavior. They then translate Lambda-React-Hook into Lambda-React-Ref, a calculus based on ML-style references, and prove that the translation preserves typability and semantics. The result shows that hook-indexed state is expressible in terms of standard references, while also illuminating the trade-offs inherent in React's design. This article reconstructs the paper's contributions in concrete detail.
Programming with React's useState
React components are functions that compute and return HTML-like elements. While basic components are pure, React provides useState to persist state across rerenders. A component that displays and increments a counter might write:
const Counter = () => {
const [x, setX] = useState(0);
return setX(x + 1)}>{x};
};
Here, useState returns both a value and a setter; the setter triggers a rerender when invoked. Crucially, useState maintains independent state for each component instance, so multiple renderings of Counter do not share state. However, the mechanism is fragile. If useState calls are placed inside a conditional, the program may crash, as hook identities are determined by evaluation order. Updating state unconditionally during rendering causes infinite rerenders. To avoid such pitfalls, React enforces the "Rules of React," such as calling hooks only at the top level of components. These rules are not statically enforceable, and a linter can only help—not guarantee soundness.
Evaluation Order and Top-Level Setter Calls
Consider the following program:
const OutOfOrder = () => {
const [x, setX] = useState(true);
if (x) {
const [y, setY] = useState(0);
}
const [z, setZ] = useState("foo");
...;
};
On the first render, the three useState calls bind initial values true, 0, and "foo" to x, y, and z respectively. If x is later set to false and the component rerenders, z will be bound to 0 instead of "foo." This happens because React identifies useState calls by their evaluation order relative to a component instance. On the second render, the second hook call occurs at a different position, shifting which location on the heap is accessed.
React avoids this class of bugs by requiring hooks to be called at the top level of components, never inside conditionals, loops, or nested functions. Custom hooks—programmer-defined compositions of primitive hooks—are also permitted at the top level. The evaluation-order invariant is the primary safeguard against misaligned state cell access.
A second pitfall arises when a setter is called unconditionally during rendering. Consider:
const CounterTwo = () => {
const [x, setX] = useState(0);
setX(x + 2);
return setX(x + 1)}>{x};
};
React detects a setter call during rendering and immediately attempts to rerender the component. Because the setter in CounterTwo is evaluated unconditionally, React will attempt this rerender indefinitely. The intent is to avoid rendering stale state, but the behavior catches novice programmers off guard. React's guidelines caution against unconditional setter calls during rendering, though the onus of verification rests on the programmer.
Lambda-React-Pure: A Stateless Core
The authors introduce three calculi. The first, Lambda-React-Pure, is stateless and can only represent static elements, but it provides the machinery necessary to support stateful components. Its syntax includes values, computations, and component bodies. Component bodies are wrapped as component{D} and include valuations, let-bindings, and a useState construct. The useState body-computation distinction syntactically enforces the top-level restriction: if-statements can only take the form if V then M else N, not if V then D1 else D2. The calculus has a small-step operational semantics and a sound type system, ensuring that well-typed programs do not get stuck.
Lambda-React-Hook: Hook-Indexed State
Lambda-React-Hook extends Lambda-React-Pure with hook-based stateful components, in the style of React. Its type system features a lightweight effect system and a pseudo-linear pointer context. The key insight is the use of a phase ϕ that is either Init or Succ. During Init, useState initializes state; during Succeed, it retrieves the most recent value from the heap. A pointer p distinguishes the state of different component instances. The step judgment ↝ϕp captures this context, and the typing rules enforce that each component instance is initialized exactly once via a phase context Φ that maps pointers to phases. Setters carry the Mut effect and are ruled out of pure bodies, preventing the infinite rerender problem.
The calculus additionally tracks state labels ℓ annotated on useState constructs, which correspond to evaluation order. The type of a body is a record 𝒞 mapping labels to value types. During Init, the body's type changes as useState disappears, but the union of the body type and the instance memory type remains constant. During Succ, every label in the body must already reside on the heap, ensuring safe retrieval.
Lambda-React-Ref: A Canonical Stateful Calculus
The third calculus, Lambda-React-Ref, extends Lambda-React-Pure with reference-based stateful components, in the style of ML. ML-style references have a well-studied formalization and are built into dialects such as OCaml, F#, and SML. The calculus provides canonicity and a small-step operational semantics over heaps. Reference cells are created, read, and updated via standard operations, and the type system tracks reference types alongside value types. This calculus serves as the target of the translation from hook-indexed state.
A Translation from Hooks to References
The central contribution is a translation from Lambda-React-Hook to Lambda-React-Ref that preserves typability and semantics. The translation is local: unrelated constructs are unaffected, and the definition is compositional. However, it involves significant code duplication, and abstraction is broken—showing an advantage of useState as a feature.
The translation begins by assigning metatypes to hook-indexed state constructs. Runtime environments maintain a heap of state cells, and location variable environments map logical locations to heap positions. The translation of a useState construct initializes or retrieves a cell on the heap depending on the phase, and the corresponding setter becomes a standard ML reference write. Because hook identity is determined by evaluation order, the translation labels each useState occurrence with a unique index, ensuring that different evaluations map to distinct heap cells. The typing rules of Lambda-React-Hook, which track the union of body and memory types across Init and Succ phases, are reflected in the type preservation proof for the translation.
Operationally, the simulation proceeds by relating the configurations of the two calculi. When a hook-indexed body reduces in the Init phase, the translation allocates a fresh reference and initializes it with the provided value. When the same body reduces in the Succ phase, the translation reads from the existing reference, yielding the stored value. Setter calls in the hook calculus translate to reference assignments in the target calculus, and the simulation proof establishes that the two reduction sequences are observationally equivalent. Type preservation follows from the invariants maintained by the phase context and the record-typing discipline.
Compositionality, Locality, and Code Duplication
The translation admits a compositional definition: the translation of a compound term is built from the translations of its subterms. It is also local: unrelated constructs—such as pure computations or unrelated hooks—are unaffected. These are desirable properties for a foundational encoding. The price is code duplication: because hook identity is tied to evaluation order, the same source term may be translated to multiple target terms depending on its position relative to other hooks. Abstraction is broken in the sense that a programmer-written useState hook cannot be fully encapsulated behind an abstract interface without exposing its indexed structure. This duplication and abstraction loss show that useState provides a more ergonomic API than its reference-based translation, even though the latter is expressible.
Extensions
The authors sketch three extensions of Lambda-React-Hook. Reconciliation addresses the problem of consistent hook evaluation order across component redefinitions; the translation approach maps hook identities to static labels, ensuring that reordering is caught statically. The useEffect hook is discussed as a mechanism for deferring side effects until after rendering, avoiding the infinite rerender problem when setter calls are needed during rendering. Custom hooks, which compose primitive hooks, are also supported; the type system enforces that custom hooks are called only at the top level, preserving the evaluation-order invariant.
Related Work and Conclusion
Prior work by Lee et al. gives a semantics for React intended as a guide for developers, whereas this paper examines useState from a language designer's perspective. Other research has explored formal models of reactive programming, but few have connected hook-indexed state to ML-style references with a machine-checked correspondence. The paper's chief contribution is showing that hook-indexed state is not a fundamentally different computational effect but can be encoded in terms of standard references, albeit with trade-offs in code duplication and abstraction. The observations inform the design of future reactive programming systems, suggesting that mechanisms resembling hook-indexed state may be elegantly captured as indexed references or effect-tracking constructs.