iterators.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 know for display purposes, collecting is fine.
  12. use dioxus::prelude::*;
  13. pub static Example: FC<()> = |cx| {
  14. let example_data = use_state(cx, || 0);
  15. let v = (0..10).map(|f| {
  16. rsx! {
  17. li { onclick: move |_| example_data.set(f)
  18. "ID: {f}"
  19. ul {
  20. {(0..10).map(|k| rsx!{
  21. li {
  22. "Sub iterator: {f}.{k}"
  23. }
  24. })}
  25. }
  26. }
  27. }
  28. });
  29. cx.render(rsx! {
  30. h3 {"Selected: {example_data}"}
  31. ul {
  32. {v}
  33. }
  34. })
  35. };