Macro leptos::with_value
source · macro_rules! with_value { (|$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
StoredValue::with_value
.
To use with signals, see the with!
macro
instead.
Note that the with!
macro also works with
StoredValue
. Use this macro if you would like to
distinguish between signals and stored values.
The general syntax looks like:
ⓘ
with_value!(|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 = store_value("Bob".to_string());
let middle = store_value("J.".to_string());
let last = store_value("Smith".to_string());
let name = with_value!(|first, middle, last| {
format!("{first} {middle} {last}")
});
assert_eq!(name, "Bob J. Smith");
The with_value!
macro in the above example expands to:
ⓘ
first.with_value(|first| {
middle.with_value(|middle| {
last.with_value(|last| format!("{first} {middle} {last}"))
})
})
If move
is added:
ⓘ
with_value!(move |first, last| format!("{first} {last}"))
Then all closures are also move
.
ⓘ
first.with_value(move |first| {
last.with_value(move |last| format!("{first} {last}"))
})