Skip to content

Target the right element when a set_focus/set_value id is built from a Var - #7904

Open
FarhanAliRaza wants to merge 10 commits into
mainfrom
ccr-b5f6d6c1-k1ong5-foreach-item-ref
Open

FarhanAliRaza wants to merge 10 commits into
mainfrom
ccr-b5f6d6c1-k1ong5-foreach-item-ref

Conversation

@FarhanAliRaza

@FarhanAliRaza FarhanAliRaza commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

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)

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?

Problem

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)),
    ),
)

Clicking "focus" or "fill" on the first row acted on the last row's input. rx.set_focus, rx.event.blur_focus and rx.set_value format the id into a ref name at compile time and drop the Var marker of the f-string, so every row sent ref: "ref_rf_item_rx_state_", a name the last rendered row had registered. Reproduced on main.

Fix

An id built from Vars is only known when the event runs, so it cannot name a ref chosen at compile time.

  • Python (reflex_base/event/__init__.py): for an id that is a Var or an f-string holding one, the three events send the id as it evaluates, in its own payload field (id: ("rf_"+item_rx_state_)). A literal id is unchanged and still sends its registered ref name (ref: "ref_input1").
  • Frontend (utils/state.js): _set_focus, _blur_focus and _set_value resolve ref through the refs registry exactly as on main, and resolve id with document.getElementById only. The two never cross, so a row id that happens to evaluate to ref_alpha, or to a name every object inherits such as toString, cannot be taken for a registered ref. An id that evaluates to undefined (a missing key) finds nothing: _set_value does nothing and the focus events log "No element found". When two elements share an id, the event acts on the first; the docs say so.

This is a different route from the one #7838 sketches (registering each row's ref under a key derived from its evaluated id). That needs a hook whose argument reads the loop variable, which is only safe where the hook is guaranteed to compile inside the loop; it is not in an @rx.memo body (#7836) and not for a component the per-item pass leaves unwrapped. Looking the element up by id needs no hook and works in every one of those places. rx.scroll_to already resolves its target with document.getElementById.

As a side effect the three events now accept a plain Var id (rx.set_focus(State.target_id)), which raised a TypeError before.

Not changed

  • A component with an f-string id still registers its (shared, unused) static ref; removing it would change what Form collects for such controls, so I left it alone.
  • A backend handler that returns rx.set_focus(f"rf_{self.selected}") passes a plain string, which is formatted to ref_rf_alpha as before and still finds nothing for an input whose id came from a Var. Supporting that needs the raw id in the payload for literal ids too, which changes the payload of every existing use, so I kept it out of this fix. Happy to follow up if you want it.

Tests

  • tests/units/reflex_base/event/test_init.py: the exact event the three functions compile to for an f-string id and for a plain Var id.
  • packages/reflex-frontend/tests/state.test.js: ref finds a registered ref by name, id finds an element by its id (including ids spelled ref_named and toString), a missing target still logs the "No element found" error, and an id that evaluates to undefined no longer throws in _set_value. Fails without the state.js change.
  • tests/integration/tests_playwright/test_server_side_event.py: the existing app gets an rx.foreach whose rows fill and focus their own input. Without the fix the first row's input stays empty.

Ran locally: tests/units (minus reflex_bench, whose browser test cannot start Chromium in my sandbox), npm run check in packages/reflex-frontend (lint, type-check, vitest), the Playwright modules test_server_side_event and test_input, plus ruff check, ruff format and pyright on the changed files.

closes #7838

https://claude.ai/code/session_01LwJUCTCgM7D4Vj6H4dRWss

…a Var

rx.set_focus, rx.blur_focus and rx.set_value named a ref by formatting the
id at compile time, with the Var marker of an f-string dropped. An id such
as f"row-{item}" inside rx.foreach therefore named one ref for every row
("ref_row_item_rx_state_"), which the last rendered row had registered:
each row's button focused or filled the last row's input.

An id built from Vars is only known when the event runs, so it is now sent
as it evaluates, and the frontend looks the element up by that id when no
ref is registered under the name. A literal id still names its registered
ref, as before. The three events also accept a plain Var id.

Fixes #7838

Claude-Session: https://claude.ai/code/session_01LwJUCTCgM7D4Vj6H4dRWss
@FarhanAliRaza
FarhanAliRaza requested review from a team and Alek99 as code owners November 15, 2026 08:45
@greptile-apps

greptile-apps Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] The change affects three browser events across Reflex apps that use them.

Rename _ref_args to satisfy the outstanding repository requirement before merging.

Findings

  1. P2 Helper name hides its purpose ▶

Summary

The PR makes focus, blur, and value events find the right element when its id comes from a Var.

  • Focus, blur, and value events target elements by Var-built ids.

No new findings. The previous helper-naming requirement remains outstanding.

Reviews (7) · Last reviewed commit: "Treat a ref event id that evaluates to n..." · Reviewed by Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 7 files

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="packages/reflex-frontend/src/utils/state.js">

<violation number="1" location="packages/reflex-frontend/src/utils/state.js:97">
P2: `resolveRef` treats inherited `Object.prototype` names as registered refs, so dynamic ids such as `"toString"` never reach `getElementById` and their focus/value operations target the inherited function instead of the row. Check only own registry keys with `Object.hasOwn(refs, ref)`.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

* itself when it is not a string.
*/
const resolveRef = (ref) => {
if (ref in refs) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: resolveRef treats inherited Object.prototype names as registered refs, so dynamic ids such as "toString" never reach getElementById and their focus/value operations target the inherited function instead of the row. Check only own registry keys with Object.hasOwn(refs, ref).

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 packages/reflex-frontend/src/utils/state.js, line 97:

<comment>`resolveRef` treats inherited `Object.prototype` names as registered refs, so dynamic ids such as `"toString"` never reach `getElementById` and their focus/value operations target the inherited function instead of the row. Check only own registry keys with `Object.hasOwn(refs, ref)`.</comment>

<file context>
@@ -85,6 +85,23 @@ export const useRegisteredRef = (name) => {
+ *     itself when it is not a string.
+ */
+const resolveRef = (ref) => {
+  if (ref in refs) {
+    return refs[ref];
+  }
</file context>
Suggested change
if (ref in refs) {
if (Object.hasOwn(refs, ref)) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 96c0897 by a different route than Object.hasOwn. An id built from a Var is now sent as id and looked up with document.getElementById only, so it never reaches the refs lookup; state.test.js covers an element whose id is toString.

The ref in refs check that remains is the expression from main. It only sees names produced by format_ref, which always start with ref_, so an inherited name cannot match it, and I left it as it was rather than widen the diff.


Generated by Claude Code

@codspeed

codspeed Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 22 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 146 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing ccr-b5f6d6c1-k1ong5-foreach-item-ref (f904d66) with main (8e0e881)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

rx.set_focus, rx.blur_focus and rx.set_value sent an id built from a Var in
the same `ref` field as the name a literal id registers its ref under, and
the frontend tried the registry first. A row id that evaluated to
"ref_alpha" therefore acted on the input with the literal id "alpha", and
one spelled like a name every object inherits, such as "toString", found
that instead of the element.

The id is now sent as `id` and looked up in the document only; `ref` names
a registered ref only, resolved as it was before. The frontend test imports
`refs` rather than reading the global.

Claude-Session: https://claude.ai/code/session_01LwJUCTCgM7D4Vj6H4dRWss
@masenf masenf added bug Something isn't working documentation Improvements or additions to documentation labels Nov 16, 2026
@FarhanAliRaza FarhanAliRaza added the on deck PRs lined up to review / merge next label Nov 30, 2026
FarhanAliRaza and others added 5 commits November 30, 2026 19:47
An id built from a Var that evaluates to undefined, such as a missing
key, left resolveRef without a ref, and _set_value read .current from
undefined. It now does nothing, as _set_focus and _blur_focus already
log and return. The missing-element message no longer calls an id a
ref, and the test restores its console spy in finally.
… a shared id targets the first element

rx.blur_focus is not exported from reflex; the event is rx.event.blur_focus.
Resolve tests/units/reflex_base/event/test_init.py by keeping both the
event chain tests from main and this branch's Var id tests.


def set_focus(ref: str) -> EventSpec:
def _ref_args(ref: str | Var[str]) -> dict[str, Var]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Helper name hides its purpose

The new helper _ref_args abbreviates its purpose. The repository requires descriptive function names instead of abbreviations. Rename it and its callers to _element_event_arguments or another full name that explains what it builds. This naming requirement must be satisfied before merging.

Rule Used: Use descriptive function names instead of abbreviations. Prefer generic names that describe functionality rather than implementation-specific names when the function might be replaced with different providers. (source)

Learned From
reflex-dev/flexgen#2190

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@FarhanAliRaza FarhanAliRaza added the priority: P1 High: confirmed bug with repro in a common feature, or high demand label Nov 30, 2026 — with Claude
masenf
masenf previously approved these changes Dec 1, 2026

@masenf masenf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a cool change, and i like how it keeps compatibility with the old mechanism.

ultimately i think we should port over the ElementAPI from reflex-enterprise, roughly:

@dataclass
class ElementAPI(PassthroughAPI):
    """Wrapper for DOM element API methods."""

    element_id: str

    @property
    def _api(self) -> JSAPIVar:
        """Get the JavaScript API object."""
        return JSAPIVar(f"document.getElementById('{self.element_id}')")

But this is heavily dependent on insecure call_script, which we can't rely on going forward. So we need some way to provide an API like this, but injecting the element into the eval environment if there's even a safe way to do so.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation on deck PRs lined up to review / merge next priority: P1 High: confirmed bug with repro in a common feature, or high demand

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants