dioxus/examples/shared_state.rs

45 lines
942 B
Rust
Raw Normal View History

#![allow(non_snake_case)]
use dioxus::prelude::*;
fn main() {
dioxus_desktop::launch(App);
}
struct DarkMode(bool);
#[rustfmt::skip]
pub fn App(cx: Scope) -> Element {
use_shared_state_provider(cx, || DarkMode(false));
render!(
DarkModeToggle {},
AppBody {}
)
}
pub fn DarkModeToggle(cx: Scope) -> Element {
let dark_mode = use_shared_state::<DarkMode>(cx).unwrap();
2023-06-27 22:17:36 +00:00
render!(input {
r#type: "checkbox",
oninput: move |event| {
let is_enabled = event.value == "true";
dark_mode.write().0 = is_enabled;
},
2023-06-27 22:17:36 +00:00
})
}
fn AppBody(cx: Scope) -> Element {
let dark_mode = use_shared_state::<DarkMode>(cx).unwrap();
let is_dark_mode = dark_mode.read().0;
let answer = if is_dark_mode { "Yes" } else { "No" };
render!(
p {
"Is Dark mode enabled? {answer}"
}
)
}