Documented <ForEnumerate/> component

This commit is contained in:
Vladi
2025-06-23 18:31:14 +02:00
parent 541dbf4dfa
commit 1fd81d1105

View File

@@ -297,3 +297,30 @@ fn main() {
</details>
</preview>
### Accessing an index while iterating with `<ForEnumerate/>`
For the cases where you need to access the real-time index while iterating,
Leptos provides a `<ForEnumerate/>` component.
The props are identical to the `<For/>` component, but when rendering `children`
it additionally provides a `ReadSignal<usize>` parameter as the index:
```rust
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
struct Counter {
id: usize,
count: RwSignal<i32>
}
<ForEnumerate
each=move || counters.get() // Same as <For/>
key=|counter| counter.id // Same as <For/>
// Provides the index as a signal and the child T
children={move |index: ReadSignal<usize>, counter: Counter| {
view! {
<button>{move || index.get()} ". Value: " {move || counter.count.get()}</button>
}
}}
/>
```