Scope hooks under rx.cond, rx.match and rx.foreach in @rx.memo bodies
All Submissions:
- Have you followed the guidelines stated in CONTRIBUTING.md file?
- Have you checked to ensure there aren't any other open Pull Requests for the desired changed?
Type of change
- Bug fix (non-breaking change which fixes an issue)
Changes To Core Features:
- Have you added an explanation of what your changes do and why you'd like us to include them?
- Have you written new tests for your core changes, as applicable?
- Have you successfully ran tests with your changes locally?
Problem
An @rx.memo body compiles into one function, with every descendant's hooks at its top (render._get_all_hooks()). For a component under rx.cond / rx.match / rx.foreach that has hooks of its own (on_mount, on_unmount, add_hooks):
rx.cond/rx.match: the hidden branch'son_mountfired when the memo mounted, and toggling the condition fired neitheron_unmountnoron_mount.rx.foreach: the hoisted hook read the loop variable outside the loop, so the page fell into the error boundary withReferenceError: item_rx_state_ is not defined, in dev and prod.
Both reproduce on main with the app from #7836.
Fix
Before the memos are compiled, each @rx.memo body goes through the pass that #7793 added for a page-level rx.foreach (_scope_instance_hooks): a descendant of a branch or a loop that has hooks of its own gets a generated memo wrapper, called where it renders, so its hooks run with the branch and once per item.
scope_memo_body_hooks(new, inreflex/compiler/plugins/memoize.py) walks a memo body down to eachCond/Match/Foreachand hands that subtree to_scope_instance_hooks. The memo's own root, and anything not under a branch or a loop, keeps its hooks at the top of the memo function. The definition inMEMOSis not mutated; the pass works on clones and returns a copy of the definition.- Memo parameter placeholders (values, event handlers, rest props) now name themselves in
VarData.deps, the way loop variables do. That is what lets a wrapper receive the parameters it reads as props, and it also puts a memo parameter into theuseCallbackdependency list of a handler that calls it. - A wrapper cannot receive the memo's
rx.RestProporchildrenas a prop (both names are taken in its own signature). A component that reads them is left where it was, so it keeps the old behaviour rather than breaking. - Client state: a wrapper takes its
useClientStatehook out of the memo function. The definition carries those names (scoped_client_state_names) so the scope the memo opens still owns them and state stays per memo instance. - The index-key warning from #7820 now says where the loop is (
in the @rx.memo component ...instead ofon page ...) when it comes from a memo body. The page wording is unchanged.
Tests
tests/units/compiler/test_memoize_plugin.py: the full compiled code of the memo and of the generated wrapper for a branch, a loop item and an event-handler parameter; the rest-props / children fallback; client state ownership; the warning text.tests/integration/tests_playwright/test_memoize_edge_cases.py: a new page in the existing app with a memo holding anrx.condand one holding anrx.foreach. Without the fix it fails withReferenceError: item_rx_state_ is not defined.
Ran locally: tests/units (minus reflex_bench, whose browser test cannot start Chromium in my sandbox), the Playwright modules test_memoize_edge_cases, test_memo (dev and prod, React Compiler off and on), test_auto_memo, test_deprecated_memo, test_client_state and test_cond_match, plus ruff check, ruff format and pyright on the changed files. pyi_hashes.json is regenerated (reflex/experimental/memo.pyi).
closes #7836
https://claude.ai/code/session_01LwJUCTCgM7D4Vj6H4dRWss
Generated by Claude Code