2023-04-09 20:42:40 +00:00
|
|
|
//! Comparison example with leptos' counter example
|
|
|
|
//! https://github.com/leptos-rs/leptos/blob/main/examples/counters/src/lib.rs
|
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
dioxus_desktop::launch(app);
|
|
|
|
}
|
|
|
|
|
2024-01-14 04:51:37 +00:00
|
|
|
fn app() -> Element {
|
2024-01-14 05:12:21 +00:00
|
|
|
let counters = use_signal(|| vec![0, 0, 0]);
|
2024-01-15 22:40:56 +00:00
|
|
|
let sum = use_selector(move || counters.read().iter().copied().sum() as usize);
|
2023-04-09 20:42:40 +00:00
|
|
|
|
|
|
|
render! {
|
|
|
|
div {
|
2024-01-15 21:06:05 +00:00
|
|
|
button { onclick: move |_| counters.write().push(0), "Add counter" }
|
|
|
|
button { onclick: move |_| { counters.write().pop(); }, "Remove counter" }
|
2023-04-09 20:42:40 +00:00
|
|
|
p { "Total: {sum}" }
|
2024-01-15 22:40:56 +00:00
|
|
|
for (i, counter) in counters.read().iter().enumerate() {
|
2023-04-09 20:42:40 +00:00
|
|
|
li {
|
2024-01-15 21:06:05 +00:00
|
|
|
button { onclick: move |_| counters.write()[i] -= 1, "-1" }
|
2023-04-09 20:42:40 +00:00
|
|
|
input {
|
|
|
|
value: "{counter}",
|
|
|
|
oninput: move |e| {
|
2023-09-01 20:38:55 +00:00
|
|
|
if let Ok(value) = e.value().parse::<usize>() {
|
2024-01-15 21:06:05 +00:00
|
|
|
counters.write()[i] = value;
|
2023-04-09 20:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-15 21:06:05 +00:00
|
|
|
button { onclick: move |_| counters.write()[i] += 1, "+1" }
|
|
|
|
button { onclick: move |_| { counters.write().remove(i); }, "x" }
|
2023-04-09 20:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|