You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/vars/custom_vars.md
+36-3Lines changed: 36 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,7 @@
1
1
```python exec
2
2
import reflex as rx
3
+
import dataclasses
4
+
from typing import TypedDict
3
5
4
6
from pcweb.pages.docs importvars
5
7
```
@@ -8,18 +10,21 @@ from pcweb.pages.docs import vars
8
10
9
11
As mentioned in the [vars page]({vars.base_vars.path}), Reflex vars must be JSON serializable.
10
12
11
-
This means we can support any Python primitive types, as well as lists, dicts, and tuples. However, you can also create more complex var types by inheriting from `rx.Base` or decorating them as dataclasses with `@dataclasses.dataclass`.
13
+
This means we can support any Python primitive types, as well as lists, dicts, and tuples. However, you can also create more complex var types using dataclasses (recommended), TypedDict, or Pydantic models.
12
14
13
15
## Defining a Type
14
16
15
-
In this example, we will create a custom var type for storing translations.
17
+
In this example, we will create a custom var type for storing translations using a dataclass.
16
18
17
19
Once defined, we can use it as a state var, and reference it from within a component.
0 commit comments