dioxus/packages/signals/examples/errors.rs

76 lines
1.7 KiB
Rust
Raw Normal View History

2023-12-16 00:35:48 +00:00
use dioxus::prelude::*;
fn main() {
// dioxus_desktop::launch(app);
2023-12-16 00:35:48 +00:00
}
#[derive(Clone, Copy)]
enum ErrorComponent {
2023-12-17 23:55:02 +00:00
Read,
ReadMut,
ReadDropped,
2023-12-16 00:35:48 +00:00
}
fn app() -> Element {
let mut error = use_signal(|| None as Option<ErrorComponent>);
2023-12-16 00:35:48 +00:00
render! {
match error() {
2023-12-17 23:55:02 +00:00
Some(ErrorComponent::Read) => render! { Read {} },
Some(ErrorComponent::ReadMut) => render! { ReadMut {} },
Some(ErrorComponent::ReadDropped) => render! { ReadDropped {} },
2023-12-16 00:35:48 +00:00
None => render! {
button { onclick: move |_| error.set(Some(ErrorComponent::Read)), "Read" }
button { onclick: move |_| error.set(Some(ErrorComponent::ReadMut)), "ReadMut" }
button { onclick: move |_| error.set(Some(ErrorComponent::ReadDropped)), "ReadDropped"}
2023-12-16 00:35:48 +00:00
}
}
}
}
#[component]
fn Read() -> Element {
let mut signal = use_signal_sync(|| 0);
2023-12-16 00:35:48 +00:00
let _write = signal.write();
let _read = signal.read();
todo!()
}
#[component]
fn ReadMut() -> Element {
let mut signal = use_signal_sync(|| 0);
2023-12-16 00:35:48 +00:00
let _read = signal.read();
let _write = signal.write();
todo!()
}
#[component]
fn ReadDropped() -> Element {
let signal = use_signal_sync(|| None as Option<SyncSignal<i32>>);
if generation() < 4 {
needs_update();
2023-12-16 00:35:48 +00:00
}
2023-12-16 00:35:48 +00:00
render! {
if let Some(value) = signal() {
"{value:?}"
2023-12-16 00:35:48 +00:00
} else {
ReadDroppedSignalChild { parent_signal: signal }
2023-12-16 00:35:48 +00:00
}
}
}
#[component]
fn ReadDroppedSignalChild(parent_signal: SyncSignal<Option<SyncSignal<i32>>>) -> Element {
let signal = use_signal_sync(|| 0);
use_hook(move || parent_signal.set(Some(signal)));
render! { "{signal}" }
2023-12-16 00:35:48 +00:00
}