pub struct ForProps<IF, I, T, EF, N, KF, K>{
pub each: IF,
pub key: KF,
pub children: EF,
}Expand description
Props for the For component.
Iterates over children and displays them, keyed by the key function given.
This is much more efficient than naively iterating over nodes with .iter().map(|n| view! { ... })...,
as it avoids re-creating DOM nodes that are not being changed.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
struct Counter {
id: usize,
count: RwSignal<i32>
}
#[component]
fn Counters() -> impl IntoView {
let (counters, set_counters) = create_signal::<Vec<Counter>>(vec![]);
view! {
<div>
<For
// a function that returns the items we're iterating over; a signal is fine
each=move || counters.get()
// a unique key for each item
key=|counter| counter.id
// renders each item to a view
children=move |counter: Counter| {
view! {
<button>"Value: " {move || counter.count.get()}</button>
}
}
/>
</div>
}
}For convenience, you can also choose to write template code directly in the <For>
component, using the let syntax:
view! {
<div>
<For
each=move || counters.get()
key=|counter| counter.id
let(counter)
>
<button>"Value: " {move || counter.count.get()}</button>
</For>
</div>
}The let syntax also supports destructuring the pattern of your data.
let((one, two)) in the case of tuples, and let(Struct { field_one, field_two })
in the case of structs.
view! {
<div>
<For
each=move || counters.get()
key=|counter| counter.id
let(Counter { id, count })
>
<button>"Value: " {move || count.get()}</button>
</For>
</div>
}§Required Props
- each: [
IF]- Items over which the component should iterate.
- key: [
KF]- A key function that will be applied to each item.
- children: [
EF]- A function that takes the item, and returns the view that will be displayed for each item.
Fields§
§each: IFItems over which the component should iterate.
key: KFA key function that will be applied to each item.
children: EFA function that takes the item, and returns the view that will be displayed for each item.
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<IF, I, T, EF, N, KF, K> Freeze for ForProps<IF, I, T, EF, N, KF, K>
impl<IF, I, T, EF, N, KF, K> RefUnwindSafe for ForProps<IF, I, T, EF, N, KF, K>
impl<IF, I, T, EF, N, KF, K> Send for ForProps<IF, I, T, EF, N, KF, K>
impl<IF, I, T, EF, N, KF, K> Sync for ForProps<IF, I, T, EF, N, KF, K>
impl<IF, I, T, EF, N, KF, K> Unpin for ForProps<IF, I, T, EF, N, KF, K>
impl<IF, I, T, EF, N, KF, K> UnwindSafe for ForProps<IF, I, T, EF, N, KF, K>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> SerializableKey for T
impl<T> SerializableKey for T
Source§impl<T> StorageAccess<T> for T
impl<T> StorageAccess<T> for T
Source§fn as_borrowed(&self) -> &T
fn as_borrowed(&self) -> &T
Borrows the value.
Source§fn into_taken(self) -> T
fn into_taken(self) -> T
Takes the value.