Skip to content

Commit b43eab0

Browse files
committed
Add use_ref documentation
1 parent fc72cf9 commit b43eab0

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

plugins/ui/docs/hooks/use_ref.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# use_ref
2+
3+
`use_ref` returns a mutable ref object whose `.current` property is initialized to the passed argument. The returned object will persist for the full lifetime of the component. Updating the `.current` property will not trigger a re-render.
4+
5+
## Example
6+
7+
```python
8+
from deephaven import ui
9+
10+
11+
@ui.component
12+
def ui_ref_counter():
13+
ref = ui.use_ref(0)
14+
15+
def handle_press():
16+
ref.current += 1
17+
print(f"You clicked {ref.current} times!")
18+
19+
return ui.button("Click me!", on_press=handle_press)
20+
21+
22+
ref_counter = ui_ref_counter()
23+
```
24+
25+
Note that we're only using the `ref.current` value within the `handle_press` method. This is because updating the ref does not trigger a re-render, so the component will not reflect the updated value. The value will be printed out each time you press the button. If you need to update the component based on the ref value, consider using `use_state` instead.
26+
27+
## Recommendations
28+
29+
1. **Use `use_ref` for mutable values**: If you need to store a mutable value that doesn't affect the component's rendering, use `use_ref`.
30+
2. **Use `use_state` for values that affect rendering**: If you need to store a value that will affect the component's rendering, use `use_state` instead of `use_ref`.
31+
32+
## Stopwatch example
33+
34+
Here's an example of a stopwatch using `use_ref`. Press the Start button to start the stopwatch, and the Stop button to stop it. The elapsed time will be displayed in the text field:
35+
36+
```python
37+
from deephaven import ui
38+
import datetime
39+
from threading import Timer
40+
41+
42+
class RepeatTimer(Timer):
43+
def run(self):
44+
while not self.finished.wait(self.interval):
45+
self.function(*self.args, **self.kwargs)
46+
47+
48+
@ui.component
49+
def ui_stopwatch():
50+
start_time, set_start_time = ui.use_state(datetime.datetime.now())
51+
now, set_now = ui.use_state(start_time)
52+
timer_ref = ui.use_ref(None)
53+
54+
def stop():
55+
if timer_ref.current is not None:
56+
timer_ref.current.cancel()
57+
58+
def start():
59+
stop()
60+
new_start_time = datetime.datetime.now()
61+
set_start_time(new_start_time)
62+
set_now(new_start_time)
63+
timer_ref.current = RepeatTimer(0.01, lambda: set_now(datetime.datetime.now()))
64+
timer_ref.current.start()
65+
66+
return ui.view(
67+
ui.heading(f"Elapsed time: {now - start_time}"),
68+
ui.button("Start", on_press=start),
69+
ui.button("Stop", on_press=stop),
70+
)
71+
72+
73+
stopwatch = ui_stopwatch()
74+
```
75+
76+
## API Reference
77+
78+
```{eval-rst}
79+
.. dhautofunction:: deephaven.ui.use_ref
80+
```

0 commit comments

Comments
 (0)