use_memo

Function use_memo 

Source
pub fn use_memo<T>(callback: impl FnMut() -> T + 'static) -> Memo<T>
where T: 'static + PartialEq,
Expand description

Registers a callback that will run every time a State which was .read() inside, changes. It also returning a type that will get cached after the callback runs, thus allowing this to be used as a way to cache expensive values.

let state = use_state(|| 0);

let expensive_value = use_memo(move || {
    // The moment `.read()` is called this side effect callback gets subscribed to it
    let value = *state.read();
    value * 2
});