2022-07-07 08:50:36 +00:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
fn main() {
|
2022-07-09 19:15:20 +00:00
|
|
|
dioxus_desktop::launch(App);
|
2022-07-07 08:50:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 23:00:12 +00:00
|
|
|
#[rustfmt::skip]
|
2022-07-07 08:50:36 +00:00
|
|
|
fn App(cx: Scope) -> Element {
|
|
|
|
let you_are_happy = true;
|
|
|
|
let you_know_it = false;
|
|
|
|
|
|
|
|
// ANCHOR: conditional
|
2023-01-06 23:00:12 +00:00
|
|
|
// ❌ don't call hooks in conditionals!
|
|
|
|
// We must ensure that the same hooks will be called every time
|
|
|
|
// But `if` statements only run if the conditional is true!
|
|
|
|
// So we might violate rule 2.
|
|
|
|
if you_are_happy && you_know_it {
|
2022-12-07 21:11:40 +00:00
|
|
|
let something = use_state(cx, || "hands");
|
2023-01-06 23:00:12 +00:00
|
|
|
println!("clap your {something}")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ✅ instead, *always* call use_state
|
|
|
|
// You can put other stuff in the conditional though
|
|
|
|
let something = use_state(cx, || "hands");
|
|
|
|
if you_are_happy && you_know_it {
|
|
|
|
println!("clap your {something}")
|
|
|
|
}
|
2022-07-07 08:50:36 +00:00
|
|
|
// ANCHOR_END: conditional
|
|
|
|
|
|
|
|
// ANCHOR: closure
|
2023-01-06 23:00:12 +00:00
|
|
|
// ❌ don't call hooks inside closures!
|
|
|
|
// We can't guarantee that the closure, if used, will be called in the same order every time
|
|
|
|
let _a = || {
|
2022-12-07 21:11:40 +00:00
|
|
|
let b = use_state(cx, || 0);
|
2023-01-06 23:00:12 +00:00
|
|
|
b.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
// ✅ instead, move hook `b` outside
|
|
|
|
let b = use_state(cx, || 0);
|
|
|
|
let _a = || b.get();
|
2022-07-07 08:50:36 +00:00
|
|
|
// ANCHOR_END: closure
|
|
|
|
|
|
|
|
let names: Vec<&str> = vec![];
|
|
|
|
|
|
|
|
// ANCHOR: loop
|
2023-01-06 23:00:12 +00:00
|
|
|
// `names` is a Vec<&str>
|
2022-07-07 08:50:36 +00:00
|
|
|
|
2023-01-06 23:00:12 +00:00
|
|
|
// ❌ Do not use hooks in loops!
|
|
|
|
// In this case, if the length of the Vec changes, we break rule 2
|
|
|
|
for _name in &names {
|
|
|
|
let is_selected = use_state(cx, || false);
|
|
|
|
println!("selected: {is_selected}");
|
|
|
|
}
|
2022-07-07 08:50:36 +00:00
|
|
|
|
2023-01-06 23:00:12 +00:00
|
|
|
// ✅ Instead, use a hashmap with use_ref
|
|
|
|
let selection_map = use_ref(cx, HashMap::<&str, bool>::new);
|
2022-07-07 08:50:36 +00:00
|
|
|
|
2023-01-06 23:00:12 +00:00
|
|
|
for name in &names {
|
|
|
|
let is_selected = selection_map.read()[name];
|
|
|
|
println!("selected: {is_selected}");
|
|
|
|
}
|
2022-07-07 08:50:36 +00:00
|
|
|
// ANCHOR_END: loop
|
|
|
|
|
2022-12-07 21:39:22 +00:00
|
|
|
cx.render(rsx!(()))
|
2022-07-07 08:50:36 +00:00
|
|
|
}
|