Test that state inheritance works as expected.
()
| 42 | |
| 43 | |
| 44 | def StateInheritance(): |
| 45 | """Test that state inheritance works as expected.""" |
| 46 | import reflex as rx |
| 47 | |
| 48 | class ChildMixin(rx.State, mixin=True): |
| 49 | child_mixin: str = "child_mixin" |
| 50 | |
| 51 | @rx.var |
| 52 | def computed_child_mixin(self) -> str: |
| 53 | return "computed_child_mixin" |
| 54 | |
| 55 | class Mixin(ChildMixin, mixin=True): |
| 56 | mixin: str = "mixin" |
| 57 | |
| 58 | @rx.var |
| 59 | def computed_mixin(self) -> str: |
| 60 | return "computed_mixin" |
| 61 | |
| 62 | @rx.event |
| 63 | def on_click_mixin(self): |
| 64 | return rx.call_script("alert('clicked')") |
| 65 | |
| 66 | class OtherMixin(rx.State, mixin=True): |
| 67 | other_mixin: str = "other_mixin" |
| 68 | other_mixin_clicks: int = 0 |
| 69 | |
| 70 | @rx.var |
| 71 | def computed_other_mixin(self) -> str: |
| 72 | return self.other_mixin |
| 73 | |
| 74 | @rx.event |
| 75 | def on_click_other_mixin(self): |
| 76 | self.other_mixin_clicks += 1 |
| 77 | self.other_mixin = ( |
| 78 | f"{type(self).__name__}.clicked.{self.other_mixin_clicks}" |
| 79 | ) |
| 80 | |
| 81 | class Base1(Mixin, rx.State): |
| 82 | _base1: str = "_base1" |
| 83 | base1: str = "base1" |
| 84 | |
| 85 | @rx.var |
| 86 | def computed_basevar(self) -> str: |
| 87 | return "computed_basevar1" |
| 88 | |
| 89 | @rx.var |
| 90 | def computed_backend_vars_base1(self) -> str: |
| 91 | return self._base1 |
| 92 | |
| 93 | class Base2(rx.State): |
| 94 | _base2: str = "_base2" |
| 95 | base2: str = "base2" |
| 96 | |
| 97 | @rx.var |
| 98 | def computed_basevar(self) -> str: |
| 99 | return "computed_basevar2" |
| 100 | |
| 101 | @rx.var |