Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Independent States #3204

Closed
benedikt-bartscher opened this issue May 1, 2024 · 0 comments
Closed

Independent States #3204

benedikt-bartscher opened this issue May 1, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@benedikt-bartscher
Copy link
Contributor

Describe the bug
All computed vars of all states get computed for every event handler execution. Even if the state class is not used at all in your reflex app.

To Reproduce
Steps to reproduce the behavior:

import reflex as rx

class StateOne(rx.State):
    @rx.var
    def computed(self) -> int:
        print(f"{type(self).__name__}.computed")
        return 1

    def do_nothing(self):
        print(f"{type(self).__name__}.do_nothing")


class StateTwo(rx.State):
    @rx.var
    def computed(self) -> int:
        print(f"{type(self).__name__}.computed")
        return 2

    def do_nothing(self):
        print(f"{type(self).__name__}.do_nothing")


def one() -> rx.Component:
    return rx.center(
        rx.button("Do nothing", on_click=StateOne.do_nothing),
        rx.text(StateOne.computed),
    )


def two() -> rx.Component:
    return rx.center(
        rx.button("Do nothing", on_click=StateTwo.do_nothing),
        rx.text(StateTwo.computed),
    )


app = rx.App()
app.add_page(one, route="/one")
# app.add_page(two, route="/two")

log output from pressing the "Do nothing" button:

StateOne.do_nothing
StateOne.computed
StateTwo.computed

Expected behavior
StateTwo does not get instantiated and StateTwo.computed does not get called for every event handler execution in other states.

Specifics (please complete the following information):

  • Python Version: 3.12.3
  • Reflex Version: main branch
  • OS: arch
@Alek99 Alek99 added the bug Something isn't working label May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants