Skip to content

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

Open

Description

@masenf

Problem

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.

Repro

import reflex as rx


class S(rx.State):
    items: list[str] = ["alpha", "beta", "gamma"]


def index():
    return rx.foreach(
        S.items,
        lambda item: rx.hstack(
            rx.input(id=f"rf_{item}"),
            rx.button("focus", on_click=rx.set_focus(f"rf_{item}")),
            rx.button("fill", on_click=rx.set_value(f"rf_{item}", item)),
        ),
    )


app = rx.App()
app.add_page(index)

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

  1. added
    bugSomething isn't working
    release-trainIssues found during agentic /prerelease-testing fanout, not necessarily validated bugs.
    on Nov 11, 2026
  2. linear-code commented on Nov 11, 2026

    @linear-code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingrelease-trainIssues found during agentic /prerelease-testing fanout, not necessarily validated bugs.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions