@@ -9,8 +9,7 @@ import reflex as rx
99# Computed Vars
1010
1111Computed vars have values derived from other properties on the backend. They are
12- defined as methods in your State class with the ` @rx.var ` decorator. A computed
13- var is recomputed every time an event is processed in your app.
12+ defined as methods in your State class with the ` @rx.var ` decorator.
1413
1514Try typing in the input box and clicking out.
1615
@@ -40,19 +39,22 @@ We recommend always using type annotations for computed vars.
4039
4140## Cached Vars
4241
43- A cached var, decorated as ` @rx.var(cache=True) ` is a special type of computed var
44- that is only recomputed when the other state vars it depends on change. This is
45- useful for expensive computations, but in some cases it may not update when you
46- expect it to.
42+ By default, all computed vars are cached (` cache=True ` ). A cached var is only
43+ recomputed when the other state vars it depends on change. This is useful for
44+ expensive computations, but in some cases it may not update when you expect it to.
45+
46+ To create a computed var that recomputes on every state update regardless of
47+ dependencies, use ` @rx.var(cache=False) ` .
48+
4749Previous versions of Reflex had a ` @rx.cached_var ` decorator, which is now replaced
48- by the new ` cache ` argument of ` @rx.var ` .
50+ by the ` cache ` argument of ` @rx.var ` (which defaults to ` True ` ) .
4951
5052``` python demo exec
5153class CachedVarState (rx .State ):
5254 counter_a: int = 0
5355 counter_b: int = 0
5456
55- @rx.var
57+ @rx.var ( cache = False )
5658 def last_touch_time (self ) -> str :
5759 # This is updated anytime the state is updated.
5860 return time.strftime(" %H:%M:%S" )
@@ -88,11 +90,11 @@ def cached_var_example():
8890 )
8991```
9092
91- In this example ` last_touch_time ` is a normal computed var, which updates any
92- time the state is modified. ` last_counter_a_update ` is a computed var that only
93- depends on ` counter_a ` , so it only gets recomputed when ` counter_a ` has changes.
94- Similarly ` last_counter_b_update ` only depends on ` counter_b ` , and thus is
95- updated only when ` counter_b ` changes.
93+ In this example ` last_touch_time ` uses ` cache=False ` to ensure it updates any
94+ time the state is modified. ` last_counter_a_update ` is a cached computed var (using
95+ the default ` cache=True ` ) that only depends on ` counter_a ` , so it only gets recomputed
96+ when ` counter_a ` changes. Similarly ` last_counter_b_update ` only depends on ` counter_b ` ,
97+ and thus is updated only when ` counter_b ` changes.
9698
9799## Async Computed Vars
98100
0 commit comments