Scope rx.foreach loop vars per item
MA
5h ago+1427+1427+0123456789012345678901234567890123456789-84-84-01234567890123456789
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)
- This change requires a documentation update
Description
Follow-up to #6936, which this PR is stacked on. It carries the rx.foreach loop-var scoping that was split out of that PR.
What changes for users
- The item and index of an
rx.foreachare available anywhere in the item's subtree, including event handlers the compiler hoists intouseCallbackand descendants it compiles into their own memo modules. Fixes #3210. - Each loop item renders through a component of its own, so an unnamed
rx.client_statevar declared in the item function is per item, and it can be seeded from the item or index. - An explicit
key=on the item is used to key the list; otherwise it is keyed by position as before. - Inside an explicit
@rx.memobody a loop is not split up, so per-item client state there still uses an@rx.memoitem function. The foreach docs say so.
Implementation
IterTagbuilds loop vars withscoped_loop_var, which reads the value from an enclosingScopedValuesprovider throughuseScopedValue; the map callback's parameter shadows the hook inside the loop body itself.Foreachrenders each item insideScopedValues, which also opens a client state scope, and no longer seals its subtree from auto-memoization.MemoizeStatefulPluginwraps each loop item in a snapshot memo component after a sealed sub-walk over the item, and a forced snapshot body is hashed by its whole subtree.
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? (unit, JS, Playwright; ruff and pyright clean)
Generated by Claude Code