route.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use dioxus_core::Element;
  2. use dioxus_core as dioxus;
  3. use dioxus_core::prelude::*;
  4. use dioxus_core_macro::Props;
  5. use dioxus_core_macro::*;
  6. use dioxus_html as dioxus_elements;
  7. use crate::{RouteContext, RouterService};
  8. #[derive(Props)]
  9. pub struct RouteProps<'a> {
  10. to: &'a str,
  11. children: Element<'a>,
  12. #[props(default)]
  13. fallback: bool,
  14. }
  15. pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element {
  16. // now we want to submit
  17. let router_root = cx
  18. .use_hook(|_| cx.consume_context::<RouterService>())
  19. .as_ref()?;
  20. cx.use_hook(|_| {
  21. // create a bigger, better, longer route if one above us exists
  22. let total_route = match cx.consume_context::<RouteContext>() {
  23. Some(ctx) => format!("{}", ctx.total_route.clone()),
  24. None => format!("{}", cx.props.to.clone()),
  25. };
  26. // provide our route context
  27. let route_context = cx.provide_context(RouteContext {
  28. declared_route: cx.props.to.to_string(),
  29. total_route,
  30. });
  31. // submit our rout
  32. router_root.register_total_route(
  33. route_context.total_route.clone(),
  34. cx.scope_id(),
  35. cx.props.fallback,
  36. );
  37. Some(RouteInner {})
  38. });
  39. log::trace!("Checking route {}", cx.props.to);
  40. if router_root.should_render(cx.scope_id()) {
  41. cx.render(rsx!(&cx.props.children))
  42. } else {
  43. None
  44. }
  45. }
  46. struct RouteInner {}
  47. impl Drop for RouteInner {
  48. fn drop(&mut self) {
  49. // todo!()
  50. }
  51. }