Repository navigation
Description
Problem
In page compilation, a component with instance hooks (on_mount / on_unmount, add_hooks, refs) that sits under rx.cond or inside rx.foreach gets its own wrapper, so its hooks run only when the branch is shown, and once per loop item (0.11 added the per-item part in #7792 / #7793). Inside the body of an @rx.memo component this does not happen: the memo body is compiled with render._get_all_hooks(), which hoists every descendant's hooks to the top of the memo function. Two visible failures follow.
rx.condin a memo body: the hidden branch'son_mountfires when the memo mounts, and toggling the condition fires nothing (neitheron_unmountof the old branch noron_mountof the new one). The 0.11 changelog line "on_mount and on_unmount still follow their rx.cond branch" holds only outside@rx.memo.rx.foreachin a memo body with item hooks: the hoisted hook references the loop variable outside the loop, so the whole page falls into the error boundary withReferenceError: it_rx_state_ is not defined(dev and prod). The changelog calls this "not covered yet"; in practice it is a page crash, not wrong counts.
Not a regression (0.10.1 behaves the same), but 0.11 documents per-row client state and memo rows, so these patterns are now encouraged.
Repro
import reflex as rx
class S(rx.State):
flag: bool = True
items: list[str] = ["alpha", "beta", "gamma"]
log: list[str] = []
@rx.event
def record(self, what: str):
self.log.append(what)
@rx.event
def toggle(self):
self.flag = not self.flag
@rx.memo
def memo_cond(show: rx.Var[bool], label: rx.Var[str]) -> rx.Component:
return rx.box(
rx.cond(
show,
rx.text("shown-", label, on_mount=S.record("m:" + label), on_unmount=S.record("u:" + label)),
)
)
@rx.memo
def memo_loop(items: rx.Var[list[str]]) -> rx.Component:
return rx.vstack(
rx.foreach(items, lambda it: rx.text(it, on_mount=S.record("m:" + it)))
)
def cond_page():
return rx.vstack(
rx.button("toggle", on_click=S.toggle),
memo_cond(show=S.flag, label="x"),
memo_cond(show=~S.flag, label="y"),
rx.text(S.log.join(",")),
)
def loop_page():
return memo_loop(items=S.items)
app = rx.App()
app.add_page(cond_page, route="/cond")
app.add_page(loop_page, route="/loop")/cond: log showsm:x,m:yat mount although onlyxis shown; clicking toggle adds nothing. Writing the samerx.conddirectly in the page (no memo) givesm:x, thenu:x,m:y, which is correct./loop: error boundary,ReferenceError: it_rx_state_ is not defined.
Expected
Hooks of a descendant under rx.cond / rx.foreach inside an @rx.memo body follow the same rules as in a page: they run in the branch / per item, not at the memo's top level.
Fix direction
- Code: the memo-body compile in
reflex/compiler/utils.py(the place that builds the memo function withhooks = render._get_all_hooks()), compared with the page path that inserts wrapper boundaries for instance-hook descendants ofCond/Foreach(the auto-memoize plugin's handling from Run the hooks of an rx.foreach item inside the loop body #7792 / Pass loop variables to auto-memo wrappers inside rx.foreach #7793). - Reuse the same boundary insertion for memo bodies: a
Condbranch orForeachitem that carries instance hooks becomes its own small component inside the memo module, so its hooks are scoped to it. - Constraints: hooks that genuinely belong to the memo's own root (not under a
Cond/Foreach) must stay at the top of the memo function; generated output must stay reproducible (no per-build random names). Watch compile time on large apps; Warn when an unkeyed rx.foreach item has lifecycle events or hooks #7820 moved a similar check into the plugin's existing walk for that reason. - If a full fix is too large, an interim compile-time error naming the memo function and the offending child is better than the runtime crash for the
rx.foreachcase. - Tests: compiler unit tests asserting the generated memo code scopes the hooks, plus a Playwright check of the two pages above.
Activity
linear-code commentedon Nov 11, 2026
ENG-13984