Skip to content

Commit

Permalink
fix: we only used the initial on_change argument of use_reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Jun 22, 2023
1 parent 8aaab96 commit a5d6b43
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion solara/hooks/use_reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def update():
updating.current = False

solara.use_memo(update, [value])
solara.use_effect(forward_on_change, [])
# if value is a reactive variable, and it changes, we need to subscribe to the latest
# reactive variable, otherwise we only link to it once
solara.use_effect(forward_on_change, [value] if isinstance(value, solara.Reactive) else [])

return reactive_value
43 changes: 43 additions & 0 deletions tests/unit/lab/toestand_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,3 +993,46 @@ def Test():
assert var2.value == 1
assert rc.find(v.Slider).widget.label == "test: 1"
rc.close()


def test_use_reactive_on_change():
control = Reactive(0)
var1 = Reactive(1)
var2 = Reactive(2)
mock1 = unittest.mock.Mock()
mock2 = unittest.mock.Mock()

@solara.component
def Test():
var: Reactive[int]
if control.value == 0:
var = solara.use_reactive(var1, on_change=mock1)
else:
var = solara.use_reactive(var2, on_change=mock2)

return solara.IntSlider("test", value=var)

box, rc = solara.render(Test(), handle_error=False)
assert rc.find(v.Slider).widget.v_model == 1
assert mock1.call_count == 0
assert mock2.call_count == 0
# if it changes downstream, it should trigger on_change
rc.find(v.Slider).widget.v_model = 10
assert mock1.call_count == 1
assert mock2.call_count == 0

control.value = 1
assert mock1.call_count == 1
assert mock2.call_count == 0
assert rc.find(v.Slider).widget.v_model == 2
rc.find(v.Slider).widget.v_model = 20
assert mock1.call_count == 1
assert mock2.call_count == 1

control.value = 0
assert rc.find(v.Slider).widget.v_model == 10
rc.find(v.Slider).widget.v_model = 100
assert mock1.call_count == 2
assert mock2.call_count == 1

rc.close()

0 comments on commit a5d6b43

Please sign in to comment.