Macro wasm_react::callback

source ·
macro_rules! callback {
    (@clones $(,)? mut $id:ident $( $tail:tt )*) => { ... };
    (@clones $(,)? $id:ident $( $tail:tt )*) => { ... };
    (@clones) => { ... };
    (clone($( $tt:tt )+), $expr:expr) => { ... };
    ($expr:expr) => { ... };
}
Expand description

Creates a new Callback which can clone-capture the environment.

Example

let cb: Callback<u64, bool> = callback!(move |arg| arg < 100);

To clone-capture the environment, specify the variables inside a clone directive:

let state = use_state(|| 0);
 
let cb = callback!(clone(mut state), move |delta: i32| {
  state.set(|c| c + delta);
});

This is equivalent to the following:

let state = use_state(|| 0);
 
let cb = {
  let mut state = state.clone();  
 
  Callback::new(move |delta: i32| {
    state.set(|c| c + delta);
  })
};