borrowed.rs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. use std::rc::Rc;
  10. struct AppProps {
  11. items: Vec<Rc<ListItem>>,
  12. }
  13. #[derive(PartialEq)]
  14. struct ListItem {
  15. name: String,
  16. age: u32,
  17. }
  18. fn app(cx: Context<AppProps>) -> DomTree {
  19. // let (val, set_val) = use_state_classic(cx, || 0);
  20. cx.render(LazyNodes::new(move |_nodecx| {
  21. todo!()
  22. // builder::ElementBuilder::new(_nodecx, "div")
  23. // .iter_child({
  24. // cx.items.iter().map(|child| {
  25. // builder::virtual_child(
  26. // _nodecx,
  27. // ChildItem,
  28. // ChildProps {
  29. // item: child.clone(),
  30. // item_handler: set_val.clone(),
  31. // },
  32. // None,
  33. // &[],
  34. // )
  35. // })
  36. // })
  37. // .iter_child([builder::ElementBuilder::new(_nodecx, "div")
  38. // .iter_child([builder::text3(_nodecx.bump(), format_args!("{}", val))])
  39. // .finish()])
  40. // .finish()
  41. }))
  42. }
  43. // props should derive a partialeq implementation automatically, but implement ptr compare for & fields
  44. struct ChildProps {
  45. // Pass down complex structs
  46. item: Rc<ListItem>,
  47. // Even pass down handlers!
  48. item_handler: Rc<dyn Fn(i32)>,
  49. }
  50. fn ChildItem<'a>(cx: Context<'a, ChildProps>) -> DomTree {
  51. cx.render(LazyNodes::new(move |__cx| todo!()))
  52. }
  53. impl PartialEq for ChildProps {
  54. fn eq(&self, _other: &Self) -> bool {
  55. false
  56. }
  57. }
  58. impl Properties for ChildProps {
  59. type Builder = ();
  60. const IS_STATIC: bool = true;
  61. fn builder() -> Self::Builder {
  62. ()
  63. }
  64. unsafe fn memoize(&self, other: &Self) -> bool {
  65. self == other
  66. }
  67. }