borrowed.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. struct ListItem {
  13. name: String,
  14. age: u32,
  15. }
  16. fn app(ctx: Context, props: &Props) -> DomTree {
  17. let (f, setter) = use_state(&ctx, || 0);
  18. ctx.render(move |b| {
  19. let mut root = builder::div(b);
  20. for child in &props.items {
  21. // notice that the child directly borrows from our vec
  22. // this makes lists very fast (simply views reusing lifetimes)
  23. root = root.child(builder::virtual_child(
  24. b,
  25. ChildProps {
  26. item: child,
  27. item_handler: setter,
  28. },
  29. // <ChildItem item=child hanldler=setter />
  30. child_item,
  31. ));
  32. }
  33. root.finish()
  34. })
  35. }
  36. type StateSetter<T> = dyn Fn(T);
  37. struct ChildProps<'a> {
  38. // Pass down complex structs
  39. item: &'a ListItem,
  40. // Even pass down handlers!
  41. item_handler: &'a StateSetter<i32>,
  42. }
  43. fn child_item(ctx: Context, props: &ChildProps) -> DomTree {
  44. todo!()
  45. // ctx.render(rsx! {
  46. // div {
  47. // item: child,
  48. // handler: setter,
  49. // abc: 123,
  50. // onclick: props.item_handler,
  51. // h1 { "abcd123" }
  52. // h2 { "abcd123" }
  53. // div {
  54. // "abcd123"
  55. // h2 { }
  56. // p { }
  57. // },
  58. // }
  59. // })
  60. }
  61. /*
  62. rsx! {
  63. ChildItem {
  64. // props
  65. item: child, handler: setter,
  66. // children
  67. div { class:"abcd", abc: 123 },
  68. div { class:"abcd", abc: 123 },
  69. // Auto-text coercion
  70. "eyo matie {abc}",
  71. // Anything that accepts Into<VChild>
  72. {},
  73. }
  74. }
  75. // dreaming of this syntax
  76. #[derive(Properties)]
  77. struct ChildProps<'a> {
  78. username: &'a str,
  79. item_handler: &'a dyn Fn(i32),
  80. }
  81. fn child_item(ctx: Context, props: &ChildProps) -> DomTree {
  82. ctx.render(rsx! {
  83. div {
  84. class: "abc123",
  85. abc: 123,
  86. onclick: props.item_handler,
  87. h1 { "Hello, {props.username}!" },
  88. h2 { "Welcome the RSX syntax" },
  89. div {
  90. h3 { "This is a subheader" }
  91. button {
  92. onclick: props.handler,
  93. "This is a button"
  94. }
  95. "This is child text"
  96. },
  97. }
  98. })
  99. }
  100. // This is also nice
  101. #[dioxus::component]
  102. static CHILD: FC = |ctx, username: &str, handler: &dyn Fn(i32)| {
  103. ctx.render(rsx! {
  104. div {
  105. class: "abc123",
  106. abc: 123,
  107. onclick: handler,
  108. h1 { "Hello, {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. Menlo, Monaco, 'Courier New', monospace
  122. struct Item {
  123. name: String,
  124. content: String,
  125. }
  126. #[dioxus::live_component]
  127. static CHILD: FC = |ctx, username: &str, handler: &dyn Fn(i32)| {
  128. // return lazy nodes or
  129. let ssr = ctx.suspend(async {
  130. let data = fetch("https://google.com")
  131. .await?
  132. .json::<Item>()
  133. .await?;
  134. rsx! {
  135. div {
  136. h1 { "Welcome: {data.name}" }
  137. p { "Content: \n {data.content}" }
  138. }
  139. }
  140. });
  141. ctx.render(rsx! {
  142. div {
  143. class: "abc123",
  144. abc: 123,
  145. onclick: handler,
  146. h1 { "Hello, {username}!" },
  147. h2 { "Welcome the RSX syntax" },
  148. div {
  149. h3 { "This is a subheader" }
  150. button {
  151. onclick: props.handler,
  152. "This is a button"
  153. }
  154. {ssr}
  155. },
  156. }
  157. })
  158. }
  159. */