borrowed.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #[derive(PartialEq)]
  13. struct ListItem {
  14. name: String,
  15. age: u32,
  16. }
  17. fn app(ctx: Context, props: &Props) -> DomTree {
  18. let (_f, setter) = use_state(&ctx, || 0);
  19. ctx.render(move |c| {
  20. let mut root = builder::ElementBuilder::new(c, "div");
  21. for child in &props.items {
  22. // notice that the child directly borrows from our vec
  23. // this makes lists very fast (simply views reusing lifetimes)
  24. // <ChildItem item=child hanldler=setter />
  25. root = root.child(builder::virtual_child(
  26. c,
  27. ChildItem,
  28. // create the props with nothing but the fc<T>
  29. fc_to_builder(ChildItem)
  30. .item(child)
  31. .item_handler(setter)
  32. .build(),
  33. ));
  34. }
  35. root.finish()
  36. })
  37. }
  38. type StateSetter<T> = dyn Fn(T);
  39. // struct StateSetter<T>(dyn Fn(T));
  40. // impl<T> PartialEq for StateSetter<T> {
  41. // fn eq(&self, other: &Self) -> bool {
  42. // self as *const _ == other as *const _
  43. // }
  44. // }
  45. // props should derive a partialeq implementation automatically, but implement ptr compare for & fields
  46. #[derive(Props)]
  47. struct ChildProps<'a> {
  48. // Pass down complex structs
  49. item: &'a ListItem,
  50. // Even pass down handlers!
  51. item_handler: &'a StateSetter<i32>,
  52. }
  53. impl PartialEq for ChildProps<'_> {
  54. fn eq(&self, _other: &Self) -> bool {
  55. // assume the dyn fn is never stable -
  56. // wrap with use_callback if it's an issue for you
  57. false
  58. }
  59. }
  60. impl<'a> Properties for ChildProps<'a> {
  61. type Builder = ChildPropsBuilder<'a, ((), ())>;
  62. fn builder() -> <Self as Properties>::Builder {
  63. ChildProps::builder()
  64. }
  65. }
  66. fn ChildItem(_ctx: Context, _props: &ChildProps) -> DomTree {
  67. todo!()
  68. // ctx.render(rsx! {
  69. // div {
  70. // item: child,
  71. // handler: setter,
  72. // abc: 123,
  73. // onclick: props.item_handler,
  74. // h1 { "abcd123" }
  75. // h2 { "abcd123" }
  76. // div {
  77. // "abcd123"
  78. // h2 { }
  79. // p { }
  80. // },
  81. // }
  82. // })
  83. }
  84. /*
  85. rsx! {
  86. ChildItem {
  87. // props
  88. item: child, handler: setter,
  89. // children
  90. div { class:"abcd", abc: 123 },
  91. div { class:"abcd", abc: 123 },
  92. // Auto-text coercion
  93. "eyo matie {abc}",
  94. // Anything that accepts Into<VChild>
  95. {},
  96. }
  97. }
  98. // dreaming of this syntax
  99. #[derive(Properties)]
  100. struct ChildProps<'a> {
  101. username: &'a str,
  102. item_handler: &'a dyn Fn(i32),
  103. }
  104. fn child_item(ctx: Context, props: &ChildProps) -> DomTree {
  105. ctx.render(rsx! {
  106. div {
  107. class: "abc123",
  108. abc: 123,
  109. onclick: props.item_handler,
  110. h1 { "Hello, {props.username}!" },
  111. h2 { "Welcome the RSX syntax" },
  112. div {
  113. h3 { "This is a subheader" }
  114. button {
  115. onclick: props.handler,
  116. "This is a button"
  117. }
  118. "This is child text"
  119. },
  120. }
  121. })
  122. }
  123. // This is also nice
  124. #[dioxus::component]
  125. static CHILD: FC = |ctx, username: &str, handler: &dyn Fn(i32)| {
  126. ctx.render(rsx! {
  127. div {
  128. class: "abc123",
  129. abc: 123,
  130. onclick: handler,
  131. h1 { "Hello, {username}!" },
  132. h2 { "Welcome the RSX syntax" },
  133. div {
  134. h3 { "This is a subheader" }
  135. button {
  136. onclick: props.handler,
  137. "This is a button"
  138. }
  139. "This is child text"
  140. },
  141. }
  142. })
  143. }
  144. Menlo, Monaco, 'Courier New', monospace
  145. struct Item {
  146. name: String,
  147. content: String,
  148. }
  149. #[dioxus::live_component]
  150. static CHILD: FC = |ctx, username: &str, handler: &dyn Fn(i32)| {
  151. // return lazy nodes or
  152. let ssr = ctx.suspend(async {
  153. let data = fetch("https://google.com")
  154. .await?
  155. .json::<Item>()
  156. .await?;
  157. rsx! {
  158. div {
  159. h1 { "Welcome: {data.name}" }
  160. p { "Content: \n {data.content}" }
  161. }
  162. }
  163. });
  164. ctx.render(rsx! {
  165. div {
  166. class: "abc123",
  167. abc: 123,
  168. onclick: handler,
  169. h1 { "Hello, {username}!" },
  170. h2 { "Welcome the RSX syntax" },
  171. div {
  172. h3 { "This is a subheader" }
  173. button {
  174. onclick: props.handler,
  175. "This is a button"
  176. }
  177. {ssr}
  178. },
  179. }
  180. })
  181. }
  182. */