borrowed.rs 5.0 KB

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