external_updates.rs 908 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. //! Example: External Updates
  2. //! -------------------------
  3. //!
  4. //! Cause updates to the VirtualDOM state from outside the component lifecycle.
  5. //! The root props could be changed or the use_receiver hook could be used.
  6. //!
  7. //!
  8. fn main() {
  9. let (recv, sender) = channel();
  10. async_std::task::spawn({
  11. for location in ["a", "b", "c", "d"] {
  12. sender.send(location);
  13. }
  14. });
  15. let app = diouxs_webview::launch_with_props(App, RootProps { recv }).await;
  16. }
  17. struct RootProps {
  18. navigator: Receiver<&'static str>,
  19. }
  20. fn App(ctx: Context, props: &RootProps) -> VNode {
  21. let router = use_router(&ctx, |router| {});
  22. let navigator = use_history(&ctx);
  23. use_receiver(&ctx, || ctx.recv.clone(), |to| navigator.navigate(to));
  24. ctx.render(rsx! {
  25. div {
  26. a { href: "/dogs/"}
  27. a { href: "/cats/"}
  28. {content}
  29. }
  30. })
  31. }