Skip to content

Commit 8149224

Browse files
Add documentation for get_var_value feature (#1329)
* Add documentation for get_var_value feature Co-Authored-By: Alek Petuskey <[email protected]> * Move get_var_value documentation to substates overview page Co-Authored-By: Alek Petuskey <[email protected]> * Make get_var_value documentation more compact and place after get_state example Co-Authored-By: Alek Petuskey <[email protected]> * Fix increment button in get_var_value example Co-Authored-By: Alek Petuskey <[email protected]> * Add more explanatory text to get_var_value example Co-Authored-By: Alek Petuskey <[email protected]> * Change button text from 'Show Count' to 'Get Count Value' Co-Authored-By: Alek Petuskey <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Alek Petuskey <[email protected]>
1 parent ad292b7 commit 8149224

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

docs/substates/overview.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,73 @@ def index():
179179
)
180180
```
181181

182+
### Accessing Individual Var Values
183+
184+
In addition to accessing entire state instances with `get_state`, you can retrieve individual variable values using the `get_var_value` method:
185+
186+
```python
187+
# Access a var value from another state
188+
value = await self.get_var_value(OtherState.some_var)
189+
```
190+
191+
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+
async def 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+
async def 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
219+
current = await self.get_var_value(CounterState.count)
220+
self.message = f"Current count: {current}"
221+
222+
def var_value_example():
223+
return rx.vstack(
224+
rx.heading("Get Var Value Example"),
225+
rx.hstack(
226+
# This button calls DisplayState.show_count to display the current count
227+
rx.button("Get Count Value", on_click=DisplayState.show_count),
228+
# This button calls CounterState.increment to increase the counter
229+
rx.button("Increment", on_click=CounterState.increment),
230+
),
231+
# Display the message from DisplayState
232+
rx.text(DisplayState.message),
233+
width="100%",
234+
align="center",
235+
spacing="4",
236+
)
237+
```
238+
239+
In this example:
240+
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 is not retrievable, `get_var_value` will raise an `UnretrievableVarValueError`.
248+
182249
## Performance Implications
183250

184251
When an event handler is called, Reflex will load the data not only for the substate containing

0 commit comments

Comments
 (0)