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