iterators.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. //! Example: Iterators
  2. //! ------------------
  3. //!
  4. //! This example demonstrates how to use iterators with Dioxus.
  5. //! Iterators must be used through the curly braces item in element bodies.
  6. //! While you might be inclined to `.collect::<>` into Html, Dioxus prefers you provide an iterator that
  7. //! resolves to VNodes. It's more efficient and easier to write than having to `collect` everywhere.
  8. //!
  9. //! This also makes it easy to write "pull"-style iterators that don't have a known size.
  10. //!
  11. //! However, when the size of an iterator needs to be known for display purposes, collecting is fine.
  12. use dioxus::prelude::*;
  13. pub static Example: Component = |cx| {
  14. let example_data = use_state(&cx, || 0);
  15. cx.render(rsx! {
  16. h3 {"Selected: {example_data}"}
  17. ul {
  18. (0..10).map(|f| rsx! {
  19. li {
  20. onclick: move |_| example_data.set(f),
  21. "ID: {f}"
  22. ul {
  23. (0..10).map(|k| rsx!{
  24. li { "Sub iterator: {f}.{k}" }
  25. })
  26. }
  27. }
  28. })
  29. }
  30. })
  31. };