reflex icon indicating copy to clipboard operation
reflex copied to clipboard

[REF-3239] Var dependency bug

Open benedikt-bartscher opened this issue 1 year ago β€’ 2 comments

Describe the bug Vars dependencies are not invalidated correctly

To Reproduce

import reflex as rx


class State(rx.State):
    @rx.var(cache=True)
    def id(self) -> str:
        return self.router.page.params.get("id", "")

    @rx.var(cache=True)
    def title(self) -> str:
        return f"Page {self.id}"


def index() -> rx.Component:
    return rx.container(
        rx.text(f"id: {State.id}"),
        rx.text(f"title: {State.title}"),
    )


app = rx.App()
app.add_page(index, route="/[id]")

Expected behavior the pages displays "id: hello" and "title: hello"

Specifics (please complete the following information):

  • Python Version: 3.12.4
  • Reflex Version: main
  • OS: arch

REF-3239

benedikt-bartscher avatar Jun 29 '24 16:06 benedikt-bartscher

try this:

class State(rx.State):
    @rx.cached_var
    def id(self) -> str:
        return self.router.page.params.get("id", "")

    @rx.var
    def title(self) -> str:
        return f"Page {self.id}"

alexlausz avatar Jun 30 '24 03:06 alexlausz

@alexlausz you must be trolling. You are suggesting to disable var caching, but this issue is about a bug in var caching. It's like telling someone who has an issue with cruise control in their car to just disable it and control the speed manually.

benedikt-bartscher avatar Jun 30 '24 10:06 benedikt-bartscher

This is important, because many var trees root in router.

abulvenz avatar Aug 26 '24 18:08 abulvenz

The example from the issue description was actually affected by shadowing (#3805 revealed it). id was overwritten by the uncached dynamically created route var.
Cached vars depending on BaseState.router do not work at all.

benedikt-bartscher avatar Aug 26 '24 22:08 benedikt-bartscher