You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
An f-string id built from an rx.foreach item gives every row the same ref; rx.set_focus / rx.set_value always target the last row #7838
Giving each row of an rx.foreach its own id from the loop variable is the natural way to focus or fill a specific row's input. The DOM ids come out distinct, but the ref registry key does not: the ref name is computed at compile time from the id string with the Var marker stripped, so every row registers the same key and the last row to render wins. rx.set_focus, rx.set_value and rx.scroll_to called with the same f-string resolve to that same static key, so every row's button acts on the last row.
Not a regression (0.10.1 behaves the same). 0.11 changed f-string ref naming for reproducible builds (#7564 / #7691 / #7707); the per-item value is still dropped from the ref name.
Click "focus" or "fill" on the first row: the third input gets focus / the value. In the compiled page every row calls useRegisteredRef("ref_rf_item_rx_state_"), while the rendered id attributes are rf_alpha, rf_beta, rf_gamma.
Expected vs actual
Expected: each row registers its ref under a key derived from the evaluated id (ref_rf_alpha, ...), and rx.set_focus(f"rf_{item}") inside the same row targets that key, so each button acts on its own row.
Actual: one shared key; the last row wins.
Fix direction
Code: Component.get_ref / _get_ref_hook in packages/reflex-base/src/reflex_base/components/component.py and format_ref in packages/reflex-base/src/reflex_base/utils/format.py; the ref-based event actions (set_focus, set_value, scroll_to) build the same name from their id argument.
When the id contains a Var (an f-string over a loop item or another Var), emit the registry key as a JS expression evaluated at render time, e.g. useRegisteredRef("ref_" + <id expr sanitized>), and have the event actions compute the key the same way at call time. useRegisteredRef already takes the name as an argument, and since Run the hooks of an rx.foreach item inside the loop body #7792 item hooks run inside the loop body, so a per-item key is reachable.
The sanitizing rule ([^\w]+ -> _) must be applied identically in Python (static ids) and JS (dynamic ids) so both sides agree.
Constraints: static ids must keep their current, reproducible ref names; no per-build random names.
Non-goals: dynamic ids that are a bare Var (id=S.some_id) currently get no ref at all (get_ref returns None); extending that is optional.
Tests: compiler unit test for the generated hook, and a Playwright test of the page above.
Activity
linear-code commentedon Nov 11, 2026
ENG-13986