Replies: 4 comments 1 reply
-
Example code: //map: HashMap<char, Counts>
<For
each = move || map.get()
key = |(k,_)| {*k as i32}
view = move |cx, (k,v)|{
view! { cx,
<div>
{k}
<div>
{v.count1}
</div>
<div>
{v.count2}
</div>
</div>
}
}
/> |
Beta Was this translation helpful? Give feedback.
-
Yes, map is ReadSignal<HashMap<char, Counts>>. I'll also attach the code where I isolated the issue. edit: To clarify, me using the struct as the key fixing the issue caused me to assume the issue was related to the key, but that's probably not true and only fixes the issue since the key changes every time the struct gets updated |
Beta Was this translation helpful? Give feedback.
-
Here's a working version of what you sent, with a few small changes:
use leptos::*;
use std::collections::HashMap;
fn main() {
mount_to_body(|cx| view! { cx, <App/> })
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
struct Counts {
count1: RwSignal<i32>,
count2: RwSignal<i32>,
}
#[component]
fn App(cx: Scope) -> impl IntoView {
let m: HashMap<char, Counts> = HashMap::new();
let (map, set_map) = create_signal(cx, m);
view! { cx,
<button on:click=move |_| {
let counts = set_map.try_update(|map| {
let counts = map
.entry('c')
.or_insert(Counts {
count1: create_rw_signal(cx, 0),
count2: create_rw_signal(cx, 0),
});
*counts
}).unwrap();
counts.count1.update(|n| *n += 1);
counts.count2.update(|n| *n += 3);
}>
"Click me: " {
let counts = move || {
map
.with(|x| {
if let Some(counts) = x.get(&'c') { counts.count1.get() } else { 0 }
})
};
log!("{}", 'f' as i32);
counts
}
</button>
<For
each=move || map.get()
key=|(k, _)| { *k as i32 }
view=move |cx, (k, v)| {
view! { cx, <div>{k} <div>{v.count1}</div> <div>{v.count2}</div></div> }
}
/>
}
} |
Beta Was this translation helpful? Give feedback.
-
Alright, I will go with that then. Thank you very much! |
Beta Was this translation helpful? Give feedback.
-
I'm fairly new to this, so I made the silly mistake of passing something as the key to the for each element that did not cause an error but prevented any nodes of the element from being updated.
As a beginner, this was quite frustrating to track down, since the nodes rendered initially but did not update.
I don't know if it is possible to prevent this or have some sort of warning but it sure would have been helpful to protect me from my own stupidity.
Beta Was this translation helpful? Give feedback.
All reactions