Function dioxus_hooks::use_memo

source ·
pub fn use_memo<T, D>(
    cx: &ScopeState,
    dependencies: D,
    callback: impl FnOnce(D::Out) -> T
) -> &Twhere
    T: 'static,
    D: UseFutureDep,
Expand description

A hook that provides a callback that executes if the dependencies change. This is useful to avoid running computation-expensive calculations even when the data doesn’t change.

  • dependencies: a tuple of references to values that are PartialEq + Clone

Examples


#[component]
fn Calculator(cx: Scope, number: usize) -> Element {
    let bigger_number = use_memo(cx, (number,), |(number,)| {
        // This will only be calculated when `number` has changed.
        number * 100
    });
    render!(
        p { "{bigger_number}" }
    )
}

#[component]
fn App(cx: Scope) -> Element {
    render!(Calculator { number: 0 })
}