Call reflex-function from rx.script

Hi!

by implementing a Paypal-Button i can use a js-script which gets the order id by JSON from Backend.

Because i am very bad in coding js, i want to do as much as possible in python.

So i tried to call reflex-function from js-code, but failed. how to do that?

"""Welcome to Reflex! This file outlines the steps to create a basic app."""

import reflex as rx

from rxconfig import config


class State(rx.State):
    """The app state."""

    blah=0
    changevar="changeme"


def index() -> rx.Component:
    return rx.container(
        rx.text("scriptcall-Test"),
        rx.html(
            '<div id="paypal-button-container" class="paypal-button-container"></div>'
        ),

        rx.html(
            '<p id="result-message">Result-Message-Contrainer</p>'
        ),

        rx.script('function dosomething(message) {  const container = document.querySelector("#result-message");  container.innerHTML = message; }'),

        rx.button(
            "Test",
            # on_click=rx.call_script('dosomething("whohoo");'), #OK!
            on_click=rx.call_script('reflexfunc();'), # _call_script ReferenceError: reflexfunc is not defined
        ),

        rx.text(State.changevar)
    )

def reflexfunc():
    State.changevar="changed"
    called=True


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

if you put reflexfunc inside your state, then you can use rx.call_script with the callback kwarg to get the return value of calling a js function

rx.call_script('dosomething("whohoo");', callback=State.reflexfunc)

maybe i’m misunderstanding your request though

Thank you, i am one step further.

In the paypal-js there is function createOrder() called, which i have to implement.

createOrder() has to return an OrderID, which i have to get from the paypal-api.

The callback basically works - i know when the paypal-button has been clicked, but i cannot return a value. With the callback parameter i am able to get values from the js but i can not write to them.

I would be fine, if i can write to a js-variable from reflex, that can be accessed by js.

So i would place a wait-loop in createOrder() and wait until the varable is set.

rx.call_script uses eval under the hood, so you would be able to write to global (window) variables in js-land. From your callback event handler, you can return rx.call_script("...") and it will execute that code on the frontend.