borrowed.rs 5.3 KB

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