borrowed.rs 908 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. //! Demonstrate that borrowed data is possible as a property type
  2. //! Borrowing (rather than cloning) is very important for speed and ergonomics.
  3. fn main() {}
  4. use dioxus_core::prelude::*;
  5. struct Props {
  6. items: Vec<ListItem>,
  7. }
  8. struct ListItem {
  9. name: String,
  10. age: u32,
  11. }
  12. fn app(ctx: Context, props: &Props) -> DomTree {
  13. ctx.view(move |b| {
  14. let mut root = builder::div(b);
  15. for child in &props.items {
  16. // notice that the child directly borrows from our vec
  17. // this makes lists very fast (simply views reusing lifetimes)
  18. root = root.child(builder::virtual_child(
  19. b,
  20. ChildProps { item: child },
  21. child_item,
  22. ));
  23. }
  24. root.finish()
  25. })
  26. }
  27. struct ChildProps<'a> {
  28. item: &'a ListItem,
  29. }
  30. fn child_item(ctx: Context, props: &ChildProps) -> DomTree {
  31. todo!()
  32. }