Skip to content

[compiler] Add experimental structured hooks lowering prototype#36305

Open
luckysolanki902 wants to merge 3 commits intofacebook:mainfrom
luckysolanki902:experimental/structured-hooks-runtime
Open

[compiler] Add experimental structured hooks lowering prototype#36305
luckysolanki902 wants to merge 3 commits intofacebook:mainfrom
luckysolanki902:experimental/structured-hooks-runtime

Conversation

@luckysolanki902
Copy link
Copy Markdown

Summary

  • add an experimental enableEmitStructuredHooks compiler flag and 'use structured hooks' directive path
  • lower a tiny structured subset of conditional useState() / useMemo() calls into experimental_useStructuredHooks(...)
  • add compiler fixtures for successful lowering and explicit rejection of unsupported hook placements
  • add a compiler integration test proving the lowered form preserves dormant branch state across hide/show rerenders
  • fix the snapshot harness so fixture @target pragmas actually control runtime-module selection
  • document the current experimental surface and its deliberately narrow limits

Why

This explores a different runtime representation for hook identity.

Today, the Rules of Hooks are enforced by a cursor-based hook model where state is identified by call order. This experiment asks whether a compiler can carve out a narrow opt-in subset where hook identity is instead assigned explicitly with stable keys.

The branch stays intentionally small:

  • only functions opting into enableEmitStructuredHooks
  • only functions using 'use structured hooks'
  • only direct variable initializers for useState() and inline useMemo() with literal dependency arrays
  • explicit errors for unsupported hooks, unsupported placements, and non-component / non-custom-hook functions

That keeps the claim tight: this does not pretend arbitrary conditional hooks are solved. It demonstrates a concrete lowering seam where branch-local hook state can survive disappear/reappear cycles while still collapsing to one real top-level hook call.

Validation

  • yarn workspace react-compiler-runtime test
  • yarn workspace babel-plugin-react-compiler run jest parseConfigPragma-test.ts structuredHooksTransform-test.ts
  • yarn workspace babel-plugin-react-compiler lint
  • yarn workspace babel-plugin-react-compiler test

@meta-cla meta-cla bot added the CLA Signed label Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant