dioxus/examples/core_reference/iterators.rs

40 lines
1.2 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-11-10 22:09:52 +00:00
pub static Example: FC<()> = |cx, props| {
2021-07-16 20:11:25 +00:00
let example_data = use_state(cx, || 0);
2021-07-12 06:23:46 +00:00
2021-07-05 05:11:49 +00:00
let v = (0..10).map(|f| {
rsx! {
2021-07-16 20:11:25 +00:00
li { onclick: move |_| example_data.set(f)
2021-07-12 06:23:46 +00:00
"ID: {f}"
ul {
{(0..10).map(|k| rsx!{
li {
"Sub iterator: {f}.{k}"
}
})}
}
2021-07-05 05:11:49 +00:00
}
}
});
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 {
2021-07-05 05:11:49 +00:00
{v}
2021-07-02 05:30:52 +00:00
}
})
};