improved shared state example
This commit is contained in:
1 file changed
+61
-28
+61
-28
@@ -1,44 +1,77 @@
|
|||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
dioxus_desktop::launch(App);
|
dioxus_desktop::launch(App);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DarkMode(bool);
|
#[derive(Default)]
|
||||||
|
struct CoolData {
|
||||||
|
data: HashMap<usize, String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CoolData {
|
||||||
|
pub fn new(data: HashMap<usize, String>) -> Self {
|
||||||
|
Self { data }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn view(&self, id: &usize) -> Option<&String> {
|
||||||
|
self.data.get(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set(&mut self, id: usize, data: String) {
|
||||||
|
self.data.insert(id, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
pub fn App(cx: Scope) -> Element {
|
pub fn App(cx: Scope) -> Element {
|
||||||
use_shared_state_provider(cx, || DarkMode(false));
|
use_shared_state_provider(cx, || CoolData::new(HashMap::from([
|
||||||
|
(0, "Hello, World!".to_string()),
|
||||||
|
(1, "Dioxus is amazing!".to_string())
|
||||||
|
])));
|
||||||
|
|
||||||
render!(
|
render!(
|
||||||
DarkModeToggle {},
|
DataEditor {
|
||||||
AppBody {}
|
id: 0
|
||||||
)
|
}
|
||||||
}
|
DataEditor {
|
||||||
|
id: 1
|
||||||
pub fn DarkModeToggle(cx: Scope) -> Element {
|
}
|
||||||
let dark_mode = use_shared_state::<DarkMode>(cx).unwrap();
|
DataView {
|
||||||
|
id: 0
|
||||||
render!(input {
|
}
|
||||||
r#type: "checkbox",
|
DataView {
|
||||||
oninput: move |event| {
|
id: 1
|
||||||
let is_enabled = event.value == "true";
|
|
||||||
dark_mode.write().0 = is_enabled;
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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}"
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline_props]
|
||||||
|
fn DataEditor(cx: Scope, id: usize) -> Element {
|
||||||
|
let cool_data = use_shared_state::<CoolData>(cx).unwrap().read();
|
||||||
|
|
||||||
|
let my_data = &cool_data.view(&id).unwrap();
|
||||||
|
|
||||||
|
render!(p {
|
||||||
|
"{my_data}"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline_props]
|
||||||
|
fn DataView(cx: Scope, id: usize) -> Element {
|
||||||
|
let cool_data = use_shared_state::<CoolData>(cx).unwrap();
|
||||||
|
|
||||||
|
let oninput = |e: FormEvent| cool_data.write().set(*id, e.value.clone());
|
||||||
|
|
||||||
|
let cool_data = cool_data.read();
|
||||||
|
let my_data = &cool_data.view(&id).unwrap();
|
||||||
|
|
||||||
|
render!(input {
|
||||||
|
oninput: oninput,
|
||||||
|
value: "{my_data}"
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in new issue
Block a user