Function consecuit::hooks::use_memo[][src]

pub fn use_memo<Deps, Res>(
    cc: HookBuilder,
    (compute, deps): (fn(_: Deps) -> Res, Deps)
) -> impl HookReturn<Res> where
    Deps: Clone + PartialEq + 'static,
    Res: Clone + 'static, 
Expand description

Memoize the computation.

Takes a single-argument function and the argument.

Return the return result of the function ran with the given argument.

The computation will be memoized; the function would only rerun when the arg chages.

The arg must be PartialEq + Clone + 'static, because we need to store and compare it.

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 find the factors of a large number:

let (cc, factors) = cc.hook(use_memo, (
    |number: i32| {
        let factors = Vec::new();
        for i in 2..number {
            if number % i == 0 {
                factors.push(i);
            }
        }
        factors
    },
    number
))