Browser Storage
rx.Cookie
Represents a state Var that is stored as a cookie in the browser. Currently only supports string values.
Parameters
name
: The name of the cookie on the client side.path
: The cookie path. Use/
to make the cookie accessible on all pages.max_age
: Relative max age of the cookie in seconds from when the client receives it.domain
: Domain for the cookie (e.g.,sub.domain.com
or.allsubdomains.com
).secure
: If the cookie is only accessible through HTTPS.same_site
: Whether the cookie is sent with third-party requests. Can be one of (True
,False
,None
,lax
,strict
).
Accessing Cookies
Cookies are accessed like any other Var in the state. If another state needs access
to the value of a cookie, the state should be a substate of the state that defines
the cookie. Alternatively the get_state
API can be used to access the other state.
For rendering cookies in the frontend, import the state that defines the cookie and reference it directly.
rx.remove_cookies
Remove a cookie from the client's browser.
Parameters:
key
: The name of cookie to remove.
This event can also be returned from an event handler:
rx.LocalStorage
Represents a state Var that is stored in localStorage in the browser. Currently only supports string values.
Parameters
name
: The name of the storage key on the client side.sync
: Boolean indicates if the state should be kept in sync across tabs of the same browser.
Syncing Vars
Because LocalStorage applies to the entire browser, all LocalStorage Vars are automatically shared across tabs.
The sync
parameter controls whether an update in one tab should be actively
propagated to other tabs without requiring a navigation or page refresh event.
rx.remove_local_storage
Remove a local storage item from the client's browser.
Parameters
key
: The key to remove from local storage.
This event can also be returned from an event handler:
rx.clear_local_storage()
Clear all local storage items from the client's browser. This may affect other apps running in the same domain or libraries within your app that use local storage.
Serialization Strategies
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.