borrowedstate.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #![allow(non_snake_case)]
  2. use dioxus::core::{ElementId, Mutation::*};
  3. use dioxus::prelude::*;
  4. #[test]
  5. fn test_borrowed_state() {
  6. let mut dom = VirtualDom::new(Parent);
  7. assert_eq!(
  8. dom.rebuild().santize().edits,
  9. [
  10. LoadTemplate { name: "template", index: 0, id: ElementId(1,) },
  11. LoadTemplate { name: "template", index: 0, id: ElementId(2,) },
  12. LoadTemplate { name: "template", index: 0, id: ElementId(3,) },
  13. HydrateText { path: &[0,], value: "Hello w1!", id: ElementId(4,) },
  14. ReplacePlaceholder { path: &[1,], m: 1 },
  15. ReplacePlaceholder { path: &[0,], m: 1 },
  16. AppendChildren { m: 1, id: ElementId(0) },
  17. ]
  18. );
  19. }
  20. fn Parent(cx: Scope) -> Element {
  21. let w1 = cx.use_hook(|| String::from("w1"));
  22. cx.render(rsx! {
  23. div {
  24. Child { name: w1 }
  25. }
  26. })
  27. }
  28. #[derive(Props)]
  29. struct ChildProps<'a> {
  30. name: &'a str,
  31. }
  32. fn Child<'a>(cx: Scope<'a, ChildProps<'a>>) -> Element {
  33. cx.render(rsx! {
  34. div {
  35. h1 { "it's nested" }
  36. Child2 { name: cx.props.name }
  37. }
  38. })
  39. }
  40. #[derive(Props)]
  41. struct Grandchild<'a> {
  42. name: &'a str,
  43. }
  44. fn Child2<'a>(cx: Scope<'a, Grandchild<'a>>) -> Element {
  45. cx.render(rsx!(div { "Hello {cx.props.name}!" }))
  46. }