macrosrc.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #![allow(unused, non_upper_case_globals)]
  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. struct Props<'a> {
  8. use_macro: bool,
  9. // todo uh not static
  10. // incorporate lifetimes into the thing somehow
  11. text: &'a str,
  12. }
  13. fn Component(ctx: Context<Props>) -> VNode {
  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. // VNodes can be constructed via a builder or the html! macro
  25. // However, both of these are "lazy" - they need to be evaluated (aka, "viewed")
  26. // We can "view" them with Context for ultimate speed while inside components
  27. if ctx.props.use_macro {
  28. ctx.view(|bump| {
  29. div(bump)
  30. .attr("class", "edit")
  31. .child(text("Hello"))
  32. .child(text(ctx.props.text))
  33. .finish()
  34. })
  35. } else {
  36. // "View" indicates exactly *when* allocations take place, everything is lazy up to this point
  37. ctx.view(html! {
  38. <div>
  39. // TODO!
  40. // Get all this working again
  41. // <h1>"Products"</h1>
  42. // // Subnodes can even be suspended
  43. // // When completely rendered, they won't cause the component itself to re-render, just their slot
  44. // <p> {product_list} </p>
  45. </div>
  46. })
  47. }
  48. }
  49. /// An example of a datafetching service
  50. async fn fetch_data() -> Result<String, ()> {
  51. todo!()
  52. }