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
This async method is particularly useful when you only need a specific value rather than loading the entire state. Using `get_var_value` can be more efficient than `get_state` when:
192
+
193
+
1. You only need to access a single variable from another state
194
+
2. The other state contains a large amount of data
195
+
3. You want to avoid loading unnecessary data into memory
196
+
197
+
Here's an example that demonstrates how to use `get_var_value` to access data between states:
198
+
199
+
```python demo exec
200
+
# Define a state that holds a counter value
201
+
class CounterState(rx.State):
202
+
# This variable will be accessed from another state
203
+
count: int=0
204
+
205
+
@rx.event
206
+
asyncdef increment(self):
207
+
# Increment the counter when the button is clicked
208
+
self.count +=1
209
+
210
+
# Define a separate state that will display information
211
+
class DisplayState(rx.State):
212
+
# This will show the current count value
213
+
message: str=""
214
+
215
+
@rx.event
216
+
asyncdef show_count(self):
217
+
# Use get_var_value to access just the count variable from CounterState
218
+
# This is more efficient than loading the entire state with get_state
1. We have two separate states: `CounterState` which manages a counter, and`DisplayState` which displays information
241
+
2. When you click "Increment", it calls `CounterState.increment()` to increase the counter value
242
+
3. When you click "Show Count", it calls `DisplayState.show_count()` which uses `get_var_value` to retrieve just the count value from`CounterState` without loading the entire state
243
+
4. The current count is then displayed in the message
244
+
245
+
This pattern is useful when you have multiple states that need to interact with each other but don't need to access all of each other's data.
246
+
247
+
If the var isnot retrievable, `get_var_value` will raise an `UnretrievableVarValueError`.
248
+
182
249
## Performance Implications
183
250
184
251
When an event handler is called, Reflex will load the data not only for the substate containing
0 commit comments