sharedstate.rs 884 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #![allow(unused, non_upper_case_globals)]
  2. use dioxus::{prelude::*, DomEdit, Mutations, TestDom};
  3. use dioxus_core as dioxus;
  4. use dioxus_core_macro::*;
  5. use dioxus_html as dioxus_elements;
  6. use DomEdit::*;
  7. mod test_logging;
  8. #[test]
  9. fn shared_state_test() {
  10. struct MySharedState(&'static str);
  11. static App: FC<()> = |(cx, props)| {
  12. cx.provide_state(MySharedState("world!"));
  13. cx.render(rsx!(Child {}))
  14. };
  15. static Child: FC<()> = |(cx, props)| {
  16. let shared = cx.consume_state::<MySharedState>()?;
  17. cx.render(rsx!("Hello, {shared.0}"))
  18. };
  19. let mut dom = VirtualDom::new(App);
  20. let Mutations { edits, .. } = dom.rebuild();
  21. assert_eq!(
  22. edits,
  23. [
  24. CreateTextNode {
  25. root: 0,
  26. text: "Hello, world!"
  27. },
  28. AppendChildren { many: 1 },
  29. ]
  30. );
  31. }