widgetti / solara

A Pure Python, React-style Framework for Scaling Your Jupyter and Web Apps
https://solara.dev
MIT License
1.9k stars 140 forks source link

`use_event` stops working with conditional rendering #300

Open iisakkirotko opened 1 year ago

iisakkirotko commented 1 year ago

Due to a bug in Reacton (see also https://github.com/widgetti/solara/pull/81) use_event can only be used at top-level in solara apps. Using conditional rendering this can be extended a bit. For instance in the following code:

import solara

background_color = solara.reactive("green")

def color(*ignore_args):
    background_color.set("red")

def reverse_color(*ignore_args):
    background_color.set("green")

@solara.component
def Page():
    el = solara.Div(
        style=f"height: 50vh; width: 50vw; background-color: {background_color.value};"
    )

    solara.v.use_event(el, "click", color)

    if background_color.value == "red":
        solara.Button("Click me", on_click=reverse_color)

use_event works normally for the first click, but stops working (without any error shown) as soon as the button is rendered, regardless of whether the button becomes unrendered later.

mariobuikhuizen commented 1 year ago

After you showed your workaround, I think this the original issue with use_event, which we workaround by having use_event and a single component in a component.

iisakkirotko commented 1 year ago

Indeed. I think the error is just getting suppressed by the conditional rendering