Macro leptos::with

source ·
macro_rules! with {
    (|$ident:ident $(,)?| $body:expr) => { ... };
    (move |$ident:ident $(,)?| $body:expr) => { ... };
    (|$first:ident, $($rest:ident),+ $(,)? | $body:expr) => { ... };
    (move |$first:ident, $($rest:ident),+ $(,)? | $body:expr) => { ... };
}
Expand description

Provides a simpler way to use SignalWith::with.

This macro also supports stored values. If you would like to distinguish between the two, you can also use with_value for stored values only.

The general syntax looks like:

with!(|capture1, capture2, ...| body);

The variables within the ‘closure’ arguments are captured from the environment, and can be used within the body with the same name.

move can also be added before the closure arguments to add move to all expanded closures.

§Examples

let (first, _) = create_signal("Bob".to_string());
let (middle, _) = create_signal("J.".to_string());
let (last, _) = create_signal("Smith".to_string());
let name = with!(|first, middle, last| format!("{first} {middle} {last}"));
assert_eq!(name, "Bob J. Smith");

The with! macro in the above example expands to:

first.with(|first| {
    middle.with(|middle| {
        last.with(|last| format!("{first} {middle} {last}"))
    })
})

If move is added:

with!(move |first, last| format!("{first} {last}"))

Then all closures are also move.

first.with(move |first| {
    last.with(move |last| format!("{first} {last}"))
})