Skip to content

Commit c88695f

Browse files
authored
fix documentation info on serializing browser storage (#1397)
1 parent 01b4718 commit c88695f

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

docs/api-reference/browser_storage.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,18 @@ rx.button(
205205

206206
# Serialization Strategies
207207

208-
If a non-trivial data structure should be stored in a `Cookie`, `LocalStorage`, or `SessionStorage` var it needs to be serialized before and after storing it. It is recommended to use a dataclass for the data which provides simple serialization helpers and works recursively in complex object structures.
208+
If a non-trivial data structure should be stored in a `Cookie`, `LocalStorage`, or `SessionStorage` var it needs to be serialized before and after storing it. It is recommended to use a pydantic class for the data which provides simple serialization helpers and works recursively in complex object structures.
209209

210210
```python demo exec
211211
import reflex as rx
212-
import dataclasses
212+
import pydantic
213213

214-
@dataclasses.dataclass
215-
class AppSettings:
214+
215+
class AppSettings(pydantic.BaseModel):
216216
theme: str = 'light'
217217
sidebar_visible: bool = True
218218
update_frequency: int = 60
219-
error_messages: list[str] = dataclasses.field(default_factory=list)
219+
error_messages: list[str] = pydantic.Field(default_factory=list)
220220

221221

222222
class ComplexLocalStorageState(rx.State):
@@ -226,12 +226,12 @@ class ComplexLocalStorageState(rx.State):
226226

227227
@rx.event
228228
def save_settings(self):
229-
self.data_raw = self.data.json()
229+
self.data_raw = self.data.model_dump_json()
230230
self.settings_open = False
231231

232232
@rx.event
233233
def open_settings(self):
234-
self.data = AppSettings.parse_raw(self.data_raw)
234+
self.data = AppSettings.model_validate_json(self.data_raw)
235235
self.settings_open = True
236236

237237
@rx.event
@@ -269,13 +269,13 @@ def app_settings():
269269
rx.form.label(
270270
"Update Frequency (seconds)",
271271
rx.input(
272-
value=ComplexLocalStorageState.data.update_frequency,
273-
on_change=lambda v: ComplexLocalStorageState.set_field(
274-
"update_frequency",
275-
v,
272+
value=ComplexLocalStorageState.data.update_frequency,
273+
on_change=lambda v: ComplexLocalStorageState.set_field(
274+
"update_frequency",
275+
v,
276+
),
276277
),
277278
),
278-
),
279279
rx.dialog.close(rx.button("Save", type="submit")),
280280
gap=2,
281281
direction="column",
@@ -291,7 +291,7 @@ def app_settings_example():
291291
),
292292
rx.dialog.content(
293293
rx.dialog.title("App Settings"),
294-
rx.dialog.description(app_settings()),
294+
app_settings(),
295295
),
296296
)
297297
```

0 commit comments

Comments
 (0)