Function consecuit::hooks::use_effect [−][src]
pub fn use_effect<Args, OnDrop>(
cc: HookBuilder,
(func, args): (fn(_: Args) -> OnDrop, Args)
) -> impl HookReturn<()> where
OnDrop: FnOnce() + 'static,
Args: PartialEq + Clone + 'static,
Expand description
Runs the function with the arg as argument when the arg changes.
Takes a single-argument function and the arg.
The arg must be PartialEq + Clone + 'static
, because we need to store and compare it.
The effect runs as the component renders. Updating states inside use_effect
will queue another render.
If you want something to run after the component completed rendering, consider using crate::executor::run_later.
This takes a function rather than a closure, so every dependency must be passed through args
.
For React devs, this is equivalent to react-hooks/exhaustive-deps
being enforced.
Example using this to change the page title when some data change:
let (cc, _) = cc.hook(use_effect, ( |deps: (String, u32)| { let title = format!("Profile - {}, age {}", deps.0, deps.1); web_sys::window().unwrap().document().unwrap().set_title(&title); }, (name, number) ));