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() {
2024-01-31 01:59:57 +00:00
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
2024-01-31 01:59:57 +00:00
rsx! {
match error() {
2024-01-31 01:59:57 +00:00
Some(ErrorComponent::Read) => rsx! { Read {} },
Some(ErrorComponent::ReadMut) => rsx! { ReadMut {} },
Some(ErrorComponent::ReadDropped) => rsx! { ReadDropped {} },
None => rsx! {
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 signal = use_signal_sync(|| 0);
2023-12-16 00:35:48 +00:00
let _write = signal.write_unchecked();
2023-12-16 00:35:48 +00:00
let _read = signal.read();
unreachable!()
2023-12-16 00:35:48 +00:00
}
#[component]
fn ReadMut() -> Element {
let signal = use_signal_sync(|| 0);
2023-12-16 00:35:48 +00:00
let _read = signal.read();
let _write = signal.write_unchecked();
2023-12-16 00:35:48 +00:00
unreachable!()
2023-12-16 00:35:48 +00:00
}
#[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
}
2024-01-31 01:59:57 +00:00
rsx! {
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)));
2024-01-31 01:59:57 +00:00
rsx! { "{signal}" }
2023-12-16 00:35:48 +00:00
}