Skip to content

fix: set_focus/blur_focus args stripped by get_handler_args for stateless handlers#6314

Merged
masenf merged 1 commit intoreflex-dev:mainfrom
FarhanAliRaza:fix-set-focus-event
Apr 9, 2026
Merged

fix: set_focus/blur_focus args stripped by get_handler_args for stateless handlers#6314
masenf merged 1 commit intoreflex-dev:mainfrom
FarhanAliRaza:fix-set-focus-event

Conversation

@FarhanAliRaza
Copy link
Copy Markdown
Collaborator

get_handler_args() used len(args) > 1 assuming all handlers have a self parameter from a State class. Server-side events like _set_focus have no state, so their single ref parameter was incorrectly stripped, producing an empty payload and a frontend TypeError: Cannot read properties of undefined (reading ref).

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

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

New Feature Submission:

  • Does your submission pass the tests?
  • Have you linted your code locally prior to submission?

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?

…less handlers

get_handler_args() used `len(args) > 1` assuming all handlers have a `self`
parameter from a State class. Server-side events like _set_focus have no state,
so their single `ref` parameter was incorrectly stripped, producing an empty
payload and a frontend TypeError: Cannot read properties of undefined (reading ref).
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 9, 2026

Greptile Summary

This PR fixes a bug in get_handler_args where stateless server-side event handlers (like _set_focus and _blur_focus) had their arguments incorrectly stripped. The function assumed all handlers have a self parameter by checking len(args) > 1, but stateless handlers have no self, so their single ref arg was dropped — producing an empty payload and a frontend TypeError. The fix aligns get_handler_args with the same n_self_args pattern already used in EventSpec.add_args and call_event_handler in the same file.

Confidence Score: 5/5

This PR is safe to merge — it's a minimal, well-targeted bug fix with matching unit and integration tests.

The one-line logic change is consistent with the same n_self_args pattern already applied in two other locations in the same file. No new API surface is introduced, the fix is backed by a unit test that directly exercises the EventChain pipeline, and an integration test confirms the DOM-level behavior. No P0 or P1 findings.

No files require special attention.

Vulnerabilities

No security concerns identified.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/event/init.py One-line targeted fix: replaces len(args) > 1 with len(args) > n_self_args in get_handler_args, using the same pattern already applied consistently elsewhere in the file.
tests/units/test_event.py Adds test_focus_event_chain_preserves_args to verify the ref arg survives EventChain.create for set_focus/blur_focus, directly covering the regression path.
tests/integration/test_server_side_event.py Adds a browser-level integration test (test_set_focus) that clicks a button calling rx.set_focus and polls for the target input becoming the active element.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["EventSpec with stateless handler\n(e.g. _set_focus, _blur_focus)"] --> B["get_handler_args(event_spec)"]
    B --> C{"handler.state is not None?"}
    C -- "Yes (State-bound handler)" --> D["n_self_args = 1"]
    C -- "No (Stateless/server-side)" --> E["n_self_args = 0"]
    D --> F{"len(args) > 1?"}
    E --> G{"len(args) > 0?"}
    F -- "True" --> H["Return event_spec.args ✅"]
    F -- "False" --> I["Return () — correctly empty"]
    G -- "True" --> J["Return event_spec.args ✅\n(ref arg preserved — BUG FIX)"]
    G -- "False" --> K["Return () — correctly empty"]
Loading

Reviews (1): Last reviewed commit: "fix: set_focus/blur_focus args stripped ..." | Re-trigger Greptile

@FarhanAliRaza FarhanAliRaza requested a review from adhami3310 April 9, 2026 17:51
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 9, 2026

Merging this PR will not alter performance

✅ 9 untouched benchmarks


Comparing FarhanAliRaza:fix-set-focus-event (3829cb1) with main (1dd2d1a)

Open in CodSpeed

@FarhanAliRaza
Copy link
Copy Markdown
Collaborator Author

The test is flaky, its fix is in work in this pr #6307

@masenf masenf merged commit 8fb2fb4 into reflex-dev:main Apr 9, 2026
39 of 40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants