borrowed.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //! Demonstrate that borrowed data is possible as a property type
  2. //! Borrowing (rather than cloning) is very important for speed and ergonomics.
  3. //!
  4. //! It's slightly more advanced than just cloning, but well worth the investment.
  5. //!
  6. //! If you use the FC macro, we handle the lifetimes automatically, making it easy to write efficient & performant components.
  7. fn main() {}
  8. use dioxus_core::prelude::*;
  9. struct Props {
  10. items: Vec<ListItem>,
  11. }
  12. struct ListItem {
  13. name: String,
  14. age: u32,
  15. }
  16. fn app(ctx: Context, props: &Props) -> DomTree {
  17. ctx.view(move |b| {
  18. let mut root = builder::div(b);
  19. for child in &props.items {
  20. // notice that the child directly borrows from our vec
  21. // this makes lists very fast (simply views reusing lifetimes)
  22. root = root.child(builder::virtual_child(
  23. b,
  24. ChildProps { item: child },
  25. child_item,
  26. ));
  27. }
  28. root.finish()
  29. })
  30. }
  31. struct ChildProps<'a> {
  32. item: &'a ListItem,
  33. }
  34. fn child_item(ctx: Context, props: &ChildProps) -> DomTree {
  35. todo!()
  36. }