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/api-reference/browser_storage.md
+98-1Lines changed: 98 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -142,9 +142,70 @@ rx.button(
142
142
)
143
143
```
144
144
145
+
## rx.SessionStorage
146
+
147
+
Represents a state Var that is stored in sessionStorage in the browser. Similar to localStorage, but the data is cleared when the page session ends (when the browser/tab is closed). Currently only supports string values.
148
+
149
+
Parameters
150
+
151
+
-`name`: The name of the storage key on the client side.
152
+
153
+
```python
154
+
classSessionStorageState(rx.State):
155
+
# session storage with default settings
156
+
s1: str= rx.SessionStorage()
157
+
158
+
# session storage with custom settings
159
+
s2: str= rx.SessionStorage("s2 default")
160
+
s3: str= rx.SessionStorage(name="s3")
161
+
```
162
+
163
+
### Session Persistence
164
+
165
+
SessionStorage data is cleared when the page session ends. A page session lasts as long as the browser is open and survives page refreshes and restores, but is cleared when the tab or browser is closed.
166
+
167
+
Unlike LocalStorage, SessionStorage is isolated to the tab/window in which it was created, so it's not shared with other tabs/windows of the same origin.
168
+
169
+
## rx.remove_session_storage
170
+
171
+
Remove a session storage item from the client's browser.
172
+
173
+
Parameters
174
+
175
+
-`key`: The key to remove from session storage.
176
+
177
+
```python
178
+
rx.button(
179
+
'Remove Session Storage',
180
+
on_click=rx.remove_session_storage('key'),
181
+
)
182
+
```
183
+
184
+
This event can also be returned from an event handler:
Clear all session storage items from the client's browser. This may affect other
196
+
apps running in the same domain or libraries within your app that use session
197
+
storage.
198
+
199
+
```python
200
+
rx.button(
201
+
'Clear all Session Storage',
202
+
on_click=rx.clear_session_storage(),
203
+
)
204
+
```
205
+
145
206
# Serialization Strategies
146
207
147
-
If a non-trivial data structure should be stored in a `Cookie`or `LocalStorage` 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 dataclass for the data which provides simple serialization helpers and works recursively in complex object structures.
148
209
149
210
```python demo exec
150
211
import reflex as rx
@@ -234,3 +295,39 @@ def app_settings_example():
234
295
),
235
296
)
236
297
```
298
+
299
+
# Comparison of Storage Types
300
+
301
+
Here's a comparison of the different client-side storage options in Reflex:
0 commit comments