Skip to main content

watch

Macro watch 

Source
watch!() { /* proc-macro */ }
Expand description

The watch! macro for creating reactive side effects.

Watches one or more signals and executes a closure whenever any of them changes. The closure is also executed once immediately with the current signal values during initialisation. This initial execution is wrapped in a suppressed-update scope so that any .set() calls inside the body do not trigger unnecessary DynamicNode re-renders.

The number of signal expressions must match the number of closure parameters. Each closure parameter receives the current value (via .get()) of the corresponding signal. Parameter types are optional and can be annotated after a colon.

let count = use_signal(|| 0_i32);
let name = use_signal(|| String::from("euv"));
watch!(count, name, |count_val: i32, name_val: String| {
    web_sys::console::log_1(&format!("count={}, name={}", count_val, name_val).into());
});