Repository navigation
Run the hooks of an rx.foreach item inside the loop body - #7792
FarhanAliRaza wants to merge 3 commits into
Conversation
A hook of a component inside rx.foreach was hoisted to the top of the Foreach memo body. A hook that read the loop variable crashed the page with a ReferenceError, and lifecycle events fired once for the whole list. When an item has such hooks, render it through ForeachItem, a runtime component that calls the item's hooks and returns its element. Fixes #7791
[Medium impact] The change affects generated frontend behavior for hooked `rx.foreach` items. The PR still needs the embedded-list rendering path fixed before merging. SummaryThe PR gives hooked The latest changes:
FarhanAliRaza documents a known limit: values declared by an outer item’s hook are not passed to an inner item unless their Reviews (3) · Last reviewed commit: "Keep an rx.foreach item component in sco..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 6 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
Merging this PR will degrade performance by 12.36%2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing |
React Compiler rejects hooks that are called in a function expression, so the render closure left the Foreach memo component unoptimized. Emit the item as a component at module level and pass the loop variables, including those of enclosing loops, as props.
There was a problem hiding this comment.
1 issue found across 4 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="tests/units/compiler/test_memoize_plugin.py">
<violation number="1" location="tests/units/compiler/test_memoize_plugin.py:565">
P2: This assertion no longer rejects the legacy `ForeachItem` runtime component, so a regression from inline rendering to that component would pass the test. Keep the broader `"ForeachItem"` check.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
| lambda: rx.foreach(SpecialFormMemoState.items, lambda item: rx.text(item)) | ||
| ) | ||
|
|
||
| assert "ForeachItem_" not in code |
There was a problem hiding this comment.
P2: This assertion no longer rejects the legacy ForeachItem runtime component, so a regression from inline rendering to that component would pass the test. Keep the broader "ForeachItem" check.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At tests/units/compiler/test_memoize_plugin.py, line 565:
<comment>This assertion no longer rejects the legacy `ForeachItem` runtime component, so a regression from inline rendering to that component would pass the test. Keep the broader `"ForeachItem"` check.</comment>
<file context>
@@ -536,7 +562,7 @@ def test_foreach_item_without_scoped_hooks_renders_inline() -> None:
)
- assert "ForeachItem" not in code
+ assert "ForeachItem_" not in code
</file context>
| assert "ForeachItem_" not in code | |
| assert "ForeachItem" not in code |
- Declare the hooks of the item key where the loop body evaluates the key. - Pass memo function parameters to the item component, next to the loop variables of enclosing loops. - Detect lifecycle events of components that an item holds in a prop.
Fixes #7791
All Submissions:
Type of change
Problem
A hook of a component inside
rx.foreachwas hoisted to the top of theForeachmemo body, outside the loop:ReferenceError: x_rx_state_ is not defined.This applied to
on_mount,on_unmount, and hooks fromadd_hooks.Fix
When an item has such hooks,
Foreachcompiles the item to a component at module level, in the same generated file, and passes the loop variables as props:The hooks belong to one item, and the loop variables are in scope. The item component also receives the variables that the enclosing generated code binds: the loop variables of enclosing
rx.foreachloops and the parameters of an enclosing@rx.memofunction. The hooks that the itemkeyneeds stay in the outer scope, because the loop body evaluates the key there.An item gets its own component only when:
_get_hooks, oradd_hooks, orAll other loops compile to the same code as before.
The item component is a normal top-level function component, so React Compiler accepts it. A first version called the hooks in a function expression inside
.map(); React Compiler rejects that ("Hooks must be called at the top level").Testing
tests/units/compiler/test_memoize_plugin.py:on_mount,add_hooksand index-variable items, a nested loop that reads the outer loop variable, a state-backed item key, a lifecycle event in a prop component, a loop inside a memo function, and an item without such hooks.tests/units/compilerandtests/units/components/core: 520 passed. All unit tests that matchmemo or foreach: 434 passed.reflex run --env prod, Chromium, default config:on_mount, also in a nested loop.on_mount. A removed item fires itson_unmount.Foreachcomponents compile, with no hook errors.Known limits
VarData, is not in scope in the item component.