dioxus/examples/core_reference/iterators.rs

35 lines
1.1 KiB
Rust
Raw Normal View History

2021-07-12 06:23:46 +00:00
//! Example: Iterators
//! ------------------
//!
//! This example demonstrates how to use iterators with Dioxus.
//! Iterators must be used through the curly braces item in element bodies.
//! While you might be inclined to `.collect::<>` into Html, Dioxus prefers you provide an iterator that
//! resolves to VNodes. It's more efficient and easier to write than having to `collect` everywhere.
//!
//! This also makes it easy to write "pull"-style iterators that don't have a known size.
2021-07-16 20:11:25 +00:00
//!
2021-10-24 17:30:36 +00:00
//! However, when the size of an iterator needs to be known for display purposes, collecting is fine.
2021-07-02 05:30:52 +00:00
2021-07-12 06:23:46 +00:00
use dioxus::prelude::*;
2021-07-16 20:11:25 +00:00
2021-12-29 04:48:25 +00:00
pub static Example: Component = |cx| {
let example_data = use_state(&cx, || 0);
2021-07-12 06:23:46 +00:00
2021-07-02 05:30:52 +00:00
cx.render(rsx! {
2021-07-16 20:11:25 +00:00
h3 {"Selected: {example_data}"}
2021-07-12 06:23:46 +00:00
ul {
(0..10).map(|f| rsx! {
li {
onclick: move |_| example_data.set(f),
"ID: {f}"
ul {
(0..10).map(|k| rsx!{
li { "Sub iterator: {f}.{k}" }
})
}
}
})
2021-07-02 05:30:52 +00:00
}
})
};