Function freya::prelude::use_effect
pub fn use_effect(callback: impl FnMut() + 'static) -> Effect
Expand description
use_effect
will subscribe to any changes in the signal values it captures
effects will always run after first mount and then whenever the signal values change
If the use_effect call was skipped due to an early return, the effect will no longer activate.
fn app() -> Element {
let mut count = use_signal(|| 0);
//the effect runs again each time count changes
use_effect(move || println!("Count changed to {count}"));
rsx! {
h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
}
}
§With non-reactive dependencies
To add non-reactive dependencies, you can use the use_reactive
hook.
Signals will automatically be added as dependencies, so you don’t need to call this method for them.
#[component]
fn Comp(count: u32) -> Element {
// Because the memo subscribes to `count` by adding it as a dependency, the memo will rerun every time `count` changes.
use_effect(use_reactive((&count, |(count,)| println!("Manually manipulate the dom") )));
todo!()
}