From 1272bd12f0d94f05123977b5d758e15ed8d98640 Mon Sep 17 00:00:00 2001 From: Alex Lazar Date: Wed, 29 Nov 2023 04:41:13 -0800 Subject: [PATCH] docs: use `with!()` macro in book and update formatting on Effect page (#2054) --- docs/book/src/reactivity/14_create_effect.md | 78 +++++++++---------- .../src/reactivity/working_with_signals.md | 2 +- 2 files changed, 39 insertions(+), 41 deletions(-) diff --git a/docs/book/src/reactivity/14_create_effect.md b/docs/book/src/reactivity/14_create_effect.md index 991074f4a..b1fc25dc4 100644 --- a/docs/book/src/reactivity/14_create_effect.md +++ b/docs/book/src/reactivity/14_create_effect.md @@ -146,13 +146,20 @@ set_num.set(2); // (nothing happens) use leptos::html::Input; use leptos::*; +#[derive(Copy, Clone)] +struct LogContext(RwSignal>); + #[component] fn App() -> impl IntoView { // Just making a visible log here // You can ignore this... let log = create_rw_signal::>(vec![]); let logged = move || log().join("\n"); - provide_context(log); + + // the newtype pattern isn't *necessary* here but is a good practice + // it avoids confusion with other possible future `RwSignal>` contexts + // and makes it easier to refer to it + provide_context(LogContext(log)); view! { @@ -169,34 +176,43 @@ fn CreateAnEffect() -> impl IntoView { // this will add the name to the log // any time one of the source signals changes create_effect(move |_| { - log( - - if use_last() { - format!("{} {}", first(), last()) - } else { - first() - }, - ) + log(if use_last() { + with!(|first, last| format!("{first} {last}")) + } else { + first() + }) }); view! { -

"create_effect" " Version"

+

+ "create_effect" + " Version" +

@@ -231,24 +247,10 @@ fn ManualVersion() -> impl IntoView { view! {

"Manual Version"

+ + - -
} @@ -273,20 +275,16 @@ fn EffectVsDerivedSignal() -> impl IntoView { move || (!my_value.with(String::is_empty)).then(|| Some(my_value.get())); view! { - +

"my_optional_value" " is " - - "Some(\"" {my_optional_value().unwrap()} "\")" + + "Some(\"" + {my_optional_value().unwrap()} + "\")"

@@ -316,7 +314,7 @@ where } fn log(msg: impl std::fmt::Display) { - let log = use_context::>>().unwrap(); + let log = use_context::().unwrap().0; log.update(|log| log.push(msg.to_string())); } diff --git a/docs/book/src/reactivity/working_with_signals.md b/docs/book/src/reactivity/working_with_signals.md index e2b57a98a..89556c917 100644 --- a/docs/book/src/reactivity/working_with_signals.md +++ b/docs/book/src/reactivity/working_with_signals.md @@ -114,7 +114,7 @@ let memoized_double_count = create_memo(move |_| count() * 2); ```rust let (first_name, set_first_name) = create_signal("Bridget".to_string()); let (last_name, set_last_name) = create_signal("Jones".to_string()); -let full_name = move || format!("{} {}", first_name(), last_name()); +let full_name = move || with!(|first_name, last_name| format!("{first_name} {last_name}")); ``` **3) A and B are independent signals, but sometimes updated at the same time.** When you make the call to update A, make a separate call to update B.