borrowedstate.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_to_vec().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!".to_string(), 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. render! {
  23. div { Child { name: w1 } }
  24. }
  25. }
  26. #[derive(Props)]
  27. struct ChildProps<'a> {
  28. name: &'a str,
  29. }
  30. fn Child<'a>(cx: Scope<'a, ChildProps<'a>>) -> Element {
  31. render! {
  32. div {
  33. h1 { "it's nested" }
  34. Child2 { name: cx.props.name }
  35. }
  36. }
  37. }
  38. #[derive(Props)]
  39. struct Grandchild<'a> {
  40. name: &'a str,
  41. }
  42. fn Child2<'a>(cx: Scope<'a, Grandchild<'a>>) -> Element {
  43. render!( div { "Hello {cx.props.name}!" } )
  44. }