external_updates.rs 906 B

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