Function freya::prelude::use_effect
pub fn use_effect<T, F, D>(
cx: &ScopeState,
dependencies: D,
future: impl FnOnce(<D as UseFutureDep>::Out) -> F
)where
T: 'static,
F: Future<Output = T> + 'static,
D: UseFutureDep,
Expand description
A hook that provides a future that executes after the hooks have been applied.
Whenever the hooks dependencies change, the future will be re-evaluated. If a future is pending when the dependencies change, the previous future will be allowed to continue.
Note: If your dependency list is always empty, use use_on_create
.
Arguments
dependencies
: a tuple of references to values that arePartialEq
+Clone
.future
: a closure that takes thedependencies
as arguments and returns a'static
future.
Examples
#[component]
fn Profile(cx: Scope, id: usize) -> Element {
let name = use_state(cx, || None);
// Only fetch the user data when the id changes.
use_effect(cx, (id,), |(id,)| {
to_owned![name];
async move {
let user = fetch_user(id).await;
name.set(user.name);
}
});
let name = name.get().clone().unwrap_or("Loading...".to_string());
render!(
p { "{name}" }
)
}
#[component]
fn App(cx: Scope) -> Element {
render!(Profile { id: 0 })
}