macrosrc.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #![allow(unused, non_upper_case_globals, non_snake_case)]
  2. use bumpalo::Bump;
  3. use dioxus_core::prelude::*;
  4. use dioxus_core::{nodebuilder::*, virtual_dom::DomTree};
  5. use std::{collections::HashMap, future::Future, marker::PhantomData};
  6. fn main() {}
  7. // ~~~ Text shared between components via props can be done with lifetimes! ~~~
  8. // Super duper efficient :)
  9. struct Props {
  10. blah: bool,
  11. text: String,
  12. }
  13. fn Component<'a>(ctx: &'a Context<Props>) -> VNode<'a> {
  14. // Write asynchronous rendering code that immediately returns a "suspended" VNode
  15. // The concurrent API will then progress this component when the future finishes
  16. // You can suspend the entire component, or just parts of it
  17. let product_list = ctx.suspend(async {
  18. // Suspend the rendering that completes when the future is done
  19. match fetch_data().await {
  20. Ok(data) => html! {<div> </div>},
  21. Err(_) => html! {<div> </div>},
  22. }
  23. });
  24. ctx.view(html! {
  25. <div>
  26. // <h1> "Products" </h1>
  27. // // Subnodes can even be suspended
  28. // // When completely rendered, they won't cause the component itself to re-render, just their slot
  29. // <p> { product_list } </p>
  30. </div>
  31. })
  32. }
  33. fn BuilderComp(ctx: Context<Props>) -> VNode {
  34. // VNodes can be constructed via a builder or the html! macro
  35. // However, both of these are "lazy" - they need to be evaluated (aka, "viewed")
  36. // We can "view" them with Context for ultimate speed while inside components
  37. ctx.view(|bump| {
  38. div(bump)
  39. .attr("class", "edit")
  40. .child(text("Hello"))
  41. .child(text(ctx.props.text.as_str()))
  42. .finish()
  43. })
  44. }
  45. #[fc]
  46. fn EffcComp(ctx: &Context, name: &str) -> VNode {
  47. // VNodes can be constructed via a builder or the html! macro
  48. // However, both of these are "lazy" - they need to be evaluated (aka, "viewed")
  49. // We can "view" them with Context for ultimate speed while inside components
  50. // use "phase" style allocation;
  51. /*
  52. nodes...
  53. text...
  54. attrs...
  55. <div> // node0
  56. <div> </div> // node1
  57. {// support some expression} // node 2
  58. </div>
  59. let node0;
  60. let node1;
  61. let node2 = evaluate{}.into();
  62. let g= |bump| {1};
  63. g(bump).into()
  64. */
  65. // should we automatically view the output or leave it?
  66. ctx.view(html! {
  67. <div>
  68. // your template goes here
  69. // feel free to directly use "name"
  70. </div>
  71. })
  72. }
  73. fn FullySuspended(ctx: Context<Props>) -> VNode {
  74. ctx.suspend(async {
  75. let i: i32 = 0;
  76. // full suspended works great with just returning VNodes!
  77. let tex = match i {
  78. 1 => html! { <div> </div> },
  79. 2 => html! { <div> </div> },
  80. _ => html! { <div> </div> },
  81. };
  82. if ctx.props.blah {
  83. html! { <div> </div> }
  84. } else {
  85. tex
  86. }
  87. })
  88. }
  89. /// An example of a datafetching service
  90. async fn fetch_data() -> Result<String, ()> {
  91. todo!()
  92. }