dioxus_router/components/
router.rs

1use crate::{provide_router_context, routable::Routable, router_cfg::RouterConfig, Outlet};
2use dioxus_core::{provide_context, use_hook, Callback, Element};
3use dioxus_core_macro::{rsx, Props};
4
5/// The props for [`Router`].
6#[derive(Props)]
7pub struct RouterProps<R: Clone + 'static> {
8    #[props(default, into)]
9    config: Callback<(), RouterConfig<R>>,
10}
11
12impl<T: Clone> Clone for RouterProps<T> {
13    fn clone(&self) -> Self {
14        *self
15    }
16}
17
18impl<T: Clone> Copy for RouterProps<T> {}
19
20impl<R: Clone + 'static> Default for RouterProps<R> {
21    fn default() -> Self {
22        Self {
23            config: Callback::new(|_| RouterConfig::default()),
24        }
25    }
26}
27
28impl<R: Clone> PartialEq for RouterProps<R> {
29    fn eq(&self, _: &Self) -> bool {
30        // prevent the router from re-rendering when the initial url or config changes
31        true
32    }
33}
34
35/// A component that renders the current route.
36pub fn Router<R: Routable + Clone>(props: RouterProps<R>) -> Element {
37    use crate::{outlet::OutletContext, RouterContext};
38
39    use_hook(|| {
40        provide_router_context(RouterContext::new(props.config.call(())));
41    });
42
43    #[cfg(feature = "streaming")]
44    dioxus_hooks::use_after_suspense_resolved(|| {
45        dioxus_fullstack_core::commit_initial_chunk();
46    });
47
48    use_hook(|| {
49        provide_context(OutletContext::<R>::new());
50    });
51
52    rsx! { Outlet::<R> {} }
53}