Skip to content
This repository was archived by the owner on Apr 28, 2026. It is now read-only.

Commit fa1ba8a

Browse files
Add SessionStorage documentation with comparison table and use cases
Co-Authored-By: Alek Petuskey <alek@pynecone.io>
1 parent 5e0277b commit fa1ba8a

1 file changed

Lines changed: 101 additions & 1 deletion

File tree

docs/api-reference/browser_storage.md

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,74 @@ rx.button(
142142
)
143143
```
144144

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+
- `sync`: Boolean indicates if the state should be kept in sync across tabs of the same browser.
153+
154+
```python
155+
class SessionStorageState(rx.State):
156+
# session storage with default settings
157+
s1: str = rx.SessionStorage()
158+
159+
# session storage with custom settings
160+
s2: str = rx.SessionStorage("s2 default")
161+
s3: str = rx.SessionStorage(name="s3")
162+
163+
# session storage that automatically updates in other states across tabs
164+
s4: str = rx.SessionStorage(sync=True)
165+
```
166+
167+
### Session Persistence
168+
169+
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.
170+
171+
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.
172+
173+
## rx.remove_session_storage
174+
175+
Remove a session storage item from the client's browser.
176+
177+
Parameters
178+
179+
- `key`: The key to remove from session storage.
180+
181+
```python
182+
rx.button(
183+
'Remove Session Storage',
184+
on_click=rx.remove_session_storage('key'),
185+
)
186+
```
187+
188+
This event can also be returned from an event handler:
189+
190+
```python
191+
class SessionStorageState(rx.State):
192+
...
193+
def logout(self):
194+
return rx.remove_session_storage('session_storage_state.s1')
195+
```
196+
197+
## rx.clear_session_storage()
198+
199+
Clear all session storage items from the client's browser. This may affect other
200+
apps running in the same domain or libraries within your app that use session
201+
storage.
202+
203+
```python
204+
rx.button(
205+
'Clear all Session Storage',
206+
on_click=rx.clear_session_storage(),
207+
)
208+
```
209+
145210
# Serialization Strategies
146211

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.
212+
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.
148213

149214
```python demo exec
150215
import reflex as rx
@@ -234,3 +299,38 @@ def app_settings_example():
234299
),
235300
)
236301
```
302+
303+
# Comparison of Storage Types
304+
305+
Here's a comparison of the different client-side storage options in Reflex:
306+
307+
| Feature | rx.Cookie | rx.LocalStorage | rx.SessionStorage |
308+
|---------|-----------|----------------|------------------|
309+
| Persistence | Until cookie expires | Until explicitly deleted | Until browser/tab is closed |
310+
| Storage Limit | ~4KB | ~5MB | ~5MB |
311+
| Sent with Requests | Yes | No | No |
312+
| Accessibility | Server & Client | Client Only | Client Only |
313+
| Expiration | Configurable | Never | End of session |
314+
| Scope | Configurable (domain, path) | Origin (domain) | Tab/Window |
315+
| Syncing Across Tabs | No | Yes (with sync=True) | No |
316+
| Use Case | Authentication, Server-side state | User preferences, App state | Temporary session data |
317+
318+
# When to Use Each Storage Type
319+
320+
## Use rx.Cookie When:
321+
- You need the data to be accessible on the server side (cookies are sent with HTTP requests)
322+
- You're handling user authentication
323+
- You need fine-grained control over expiration and scope
324+
- You need to limit the data to specific paths in your app
325+
326+
## Use rx.LocalStorage When:
327+
- You need to store larger amounts of data (up to ~5MB)
328+
- You want the data to persist indefinitely (until explicitly deleted)
329+
- You need to share data between different tabs/windows of your app
330+
- You want to store user preferences that should be remembered across browser sessions
331+
332+
## Use rx.SessionStorage When:
333+
- You need temporary data that should be cleared when the browser/tab is closed
334+
- You want to isolate data to a specific tab/window
335+
- You're storing sensitive information that shouldn't persist after the session ends
336+
- You're implementing per-session features like form data, shopping carts, or multi-step processes

0 commit comments

Comments
 (0)