borrowed.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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(dioxus::prelude::LazyNodes::new(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. fn ChildItem(_ctx: Context, _props: &ChildProps) -> DomTree {
  61. todo!()
  62. // ctx.render(rsx! {
  63. // div {
  64. // item: child,
  65. // handler: setter,
  66. // abc: 123,
  67. // onclick: props.item_handler,
  68. // h1 { "abcd123" }
  69. // h2 { "abcd123" }
  70. // div {
  71. // "abcd123"
  72. // h2 { }
  73. // p { }
  74. // },
  75. // }
  76. // })
  77. }
  78. /*
  79. rsx! {
  80. ChildItem {
  81. // props
  82. item: child, handler: setter,
  83. // children
  84. div { class:"abcd", abc: 123 },
  85. div { class:"abcd", abc: 123 },
  86. // Auto-text coercion
  87. "eyo matie {abc}",
  88. // Anything that accepts Into<VChild>
  89. {},
  90. }
  91. }
  92. // dreaming of this syntax
  93. #[derive(Properties)]
  94. struct ChildProps<'a> {
  95. username: &'a str,
  96. item_handler: &'a dyn Fn(i32),
  97. }
  98. fn child_item(ctx: Context, props: &ChildProps) -> DomTree {
  99. ctx.render(rsx! {
  100. div {
  101. class: "abc123",
  102. abc: 123,
  103. onclick: props.item_handler,
  104. h1 { "Hello, {props.username}!" },
  105. h2 { "Welcome the RSX syntax" },
  106. div {
  107. h3 { "This is a subheader" }
  108. button {
  109. onclick: props.handler,
  110. "This is a button"
  111. }
  112. "This is child text"
  113. },
  114. }
  115. })
  116. }
  117. // This is also nice
  118. #[dioxus::component]
  119. static CHILD: FC = |ctx, username: &str, handler: &dyn Fn(i32)| {
  120. ctx.render(rsx! {
  121. div {
  122. class: "abc123",
  123. abc: 123,
  124. onclick: handler,
  125. h1 { "Hello, {username}!" },
  126. h2 { "Welcome the RSX syntax" },
  127. div {
  128. h3 { "This is a subheader" }
  129. button {
  130. onclick: props.handler,
  131. "This is a button"
  132. }
  133. "This is child text"
  134. },
  135. }
  136. })
  137. }
  138. Menlo, Monaco, 'Courier New', monospace
  139. struct Item {
  140. name: String,
  141. content: String,
  142. }
  143. #[dioxus::live_component]
  144. static CHILD: FC = |ctx, username: &str, handler: &dyn Fn(i32)| {
  145. // return lazy nodes or
  146. let ssr = ctx.suspend(async {
  147. let data = fetch("https://google.com")
  148. .await?
  149. .json::<Item>()
  150. .await?;
  151. rsx! {
  152. div {
  153. h1 { "Welcome: {data.name}" }
  154. p { "Content: \n {data.content}" }
  155. }
  156. }
  157. });
  158. ctx.render(rsx! {
  159. div {
  160. class: "abc123",
  161. abc: 123,
  162. onclick: handler,
  163. h1 { "Hello, {username}!" },
  164. h2 { "Welcome the RSX syntax" },
  165. div {
  166. h3 { "This is a subheader" }
  167. button {
  168. onclick: props.handler,
  169. "This is a button"
  170. }
  171. {ssr}
  172. },
  173. }
  174. })
  175. }
  176. */