Skip to content

Commit 31f45dc

Browse files
committed
Copilot suggestions
1 parent c14aebc commit 31f45dc

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

docs/state_structure/shared_state.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ An `rx.SharedState` subclass has two attributes for determining link status and
138138

139139
Provides the token that the state is currently linked to, or empty string if not linked.
140140

141-
This attribute is only set on the linked state instance returned by `_link_to`. It will be an empty string on any unlinked shared state instances. However, if another state links to a client's private token, then the `_linked_to` attribute will be set to the client's token rather than empty string. This is yet another reason to avoid linking to client tokens directly.
141+
This attribute is only set on the linked state instance returned by `_link_to`. It will be an empty string on any unlinked shared state instances. However, if another state links to a client's private token, then the `_linked_to` attribute will be set to the client's token rather than an empty string.
142+
143+
When `_linked_to` equals `self.router.session.client_token`, it is assumed that the current client is unlinked, but another client has linked to this client's private state. Although this is possible, it is generally discouraged to link shared states to private client tokens.
142144

143145
**`_linked_from: set[str]`**
144146

@@ -159,6 +161,8 @@ Linked states are always loaded into the tree for each event on each linked clie
159161
A shared state should primarily use backend-only vars (prefixed with an underscore) to store shared data. Often, not all users of the shared state need visibility into all of the data in the shared state. Use computed vars to provide sanitized access to shared data as needed.
160162

161163
```python
164+
from typing import Literal
165+
162166
class SharedGameState(rx.SharedState):
163167
# Sensitive user metadata stored in backend-only variable.
164168
_players: dict[str, Literal["X", "O"]] = {}
@@ -197,11 +201,15 @@ It is often convenient to define dynamic routes that include the linked token as
197201
```python
198202
class SharedRoom(rx.SharedState):
199203
async def on_load(self):
204+
# `self.room_id` is the automatically defined dynamic route var.
200205
await self._link_to(self.room_id.replace("_", "-") or "default-room")
201206

202207

208+
def room_page(): ...
209+
210+
203211
app.add_route(
204-
room,
212+
room_page,
205213
path="/room/[room_id]",
206214
on_load=SharedRoom.on_load,
207215
)

0 commit comments

Comments
 (0)