Skip to content

make on load lambda work#5856

Merged
adhami3310 merged 2 commits intomainfrom
make-on-load-lambda-work
Oct 8, 2025
Merged

make on load lambda work#5856
adhami3310 merged 2 commits intomainfrom
make-on-load-lambda-work

Conversation

@adhami3310
Copy link
Member

@adhami3310 adhami3310 commented Oct 6, 2025

import reflex as rx


def index() -> rx.Component:
    return rx.container()


app = rx.App()
app.add_page(index, "/", on_load=lambda: rx.redirect("https://reflex.dev/docs"))

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Greptile Overview

Summary

This PR introduces automatic lambda invocation in Reflex's event handling system. The change modifies the `fix_events` function in `reflex/event.py` to detect when an event handler returns a lambda function and automatically call it with no arguments.

The implementation adds a simple check that identifies lambda functions by their __qualname__ attribute being equal to '<lambda>'. When this condition is met, the lambda is immediately invoked with no parameters. This change specifically targets a common user pattern where developers return lambda functions from on_load event handlers expecting them to be automatically executed.

This enhancement integrates seamlessly with Reflex's existing event processing pipeline, sitting within the event normalization logic that already handles various event types and formats. The change maintains backward compatibility since it only affects lambda functions and doesn't alter the behavior of other callable objects or regular functions.

Important Files Changed

Changed Files
Filename Score Overview
reflex/event.py 4/5 Adds automatic lambda detection and invocation in the fix_events function

Confidence score: 4/5

  • This PR is relatively safe to merge with minimal risk of breaking existing functionality
  • Score reflects the targeted nature of the change and the safe lambda detection mechanism using __qualname__
  • Pay close attention to the event handling logic in reflex/event.py to ensure lambda detection works as expected

Sequence Diagram

sequenceDiagram
    participant User
    participant "Event System" as ES
    participant "call_script" as CS
    participant Frontend
    participant "Event Handler" as EH

    User->>ES: "Create event with lambda callback"
    ES->>CS: "call_script(js_code, callback_lambda)"
    CS->>CS: "Check if callback is provided"
    alt callback is EventHandler
        CS->>CS: "call_event_handler(callback, _callback_arg_spec)"
        CS->>EH: "Process EventHandler callback"
        EH-->>CS: "Return processed event"
    else callback is Lambda/FunctionType
        CS->>CS: "call_event_fn(callback, _callback_arg_spec)"
        Note over CS: "Lambda now properly handled for on_load"
        CS->>CS: "Parse lambda args and call with result"
        CS-->>CS: "Return lambda result as event"
    else callback is invalid
        CS->>CS: "Raise ValueError"
        CS-->>User: "Error: Invalid callback type"
    end
    CS->>CS: "server_side('_call_script', signature, js_code, callback_kwargs)"
    CS-->>ES: "Return EventSpec"
    ES->>Frontend: "Execute JavaScript code"
    Frontend->>Frontend: "Evaluate JavaScript"
    alt callback exists
        Frontend->>EH: "Send result to callback handler"
        EH->>EH: "Process callback with result"
    end
    Frontend-->>User: "JavaScript execution complete"
Loading

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@codspeed-hq
Copy link

codspeed-hq bot commented Oct 6, 2025

CodSpeed Performance Report

Merging #5856 will not alter performance

Comparing make-on-load-lambda-work (6612a1f) with main (b7230bd)

Summary

✅ 8 untouched

@adhami3310 adhami3310 merged commit cdfa8b5 into main Oct 8, 2025
50 of 53 checks passed
@adhami3310 adhami3310 deleted the make-on-load-lambda-work branch October 8, 2025 18:21
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