iterators.rs 1.0 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. fn main() {}
  11. use dioxus::prelude::*;
  12. static Example: FC<()> = |cx| {
  13. let g = use_state(cx, || 0);
  14. let v = (0..10).map(|f| {
  15. rsx! {
  16. li {
  17. onclick: move |_| g.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: {g}"}
  31. ul {
  32. {v}
  33. }
  34. })
  35. };