AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

React Compiler + Turbopack: Lazy UseState Initializer Hits TDZ When Helper Function Declarations Are Lowered To Const

Next.js 16.3.5 with reactCompiler and experimental.turbopackRustReactCompiler throws a runtime ReferenceError (Cannot access 'parseItem' before initialization) because React Compiler lowers hoisted function declarations to const bindings while leaving lazy useState initializers above them, breaking valid JavaScript hoisting.

highConfidence 92%Next.jsAffected V16.3.5

Origin Analysis

React Compiler's SWC/Rust transform in Turbopack lowers nested function declarations to const bindings without preserving hoisting semantics. When useState receives a lazy initializer that calls a function declared later in the same hook, the initializer executes before the const binding is initialized, causing a temporal dead zone (TDZ) ReferenceError.
1. Clone https://github.com/michaltarasiuk/usestate-tdz-repro 2. Run bun install 3. In next.config.ts ensure reactCompiler: true and experimental.turbopackRustReactCompiler: true 4. Run bun dev (Turbopack default in Next.js 16) 5. Open http://localhost:3000 6. Observe runtime error overlay: Runtime ReferenceError: Cannot access 'parseItem' before initialization

Fixing Code Block

Edge Case Audit

This is a user-code workaround, not a compiler fix. Any remaining hoisted function declarations used by lazy initializers may still trigger the same TDZ. If upgrading or downgrading Next.js changes compiler behavior, verify all hooks. To rollback, delete .next and disable experimental.turbopackRustReactCompiler (or set reactCompiler: false) in next.config.ts.

Ecosystem Topology