add as_mapped_ref

This commit is contained in:
Evan Almloff committed 2024-01-01 10:44:21 -06:00
1 parent e45739ac04
commit b9d179c494
6 files changed
+68 -20

No files matched your search

+1 -1
View File
@@ -23,6 +23,6 @@ fn App(cx: Scope) -> Element {
}
#[component]
fn Child(cx: Scope, signal: SignalMap<String>) -> Element {
fn Child(cx: Scope, signal: MappedSignal<String>) -> Element {
render! {"{signal:?}"}
}
+39
View File
@@ -0,0 +1,39 @@
use dioxus::prelude::*;
use dioxus_signals::*;
fn main() {
dioxus_desktop::launch(App);
}
#[component]
fn App(cx: Scope) -> Element {
let signal = use_signal(cx, || Some(String::from("Hello")));
render! {
button {
onclick: move |_| {
let new_value = (!signal.read().is_some()).then(|| String::from("Hello"));
signal.set(new_value);
},
"Swap"
}
button {
onclick: move |_| {
if let Some(value) = &mut *signal.write() {
value.push_str(" World");
}
},
"Change"
}
if let Some(item) = signal.as_mapped_ref(){
render! {
Child { signal: item }
}
}
}
}
#[component]
fn Child(cx: Scope, signal: MappedSignal<String>) -> Element {
render! {"{signal:?}"}
}