dioxus_router/components/
outlet.rs

1use crate::prelude::{outlet::OutletContext, *};
2use dioxus_lib::prelude::*;
3
4/// An outlet for the current content.
5///
6/// Only works as descendant of a [`Link`] component, otherwise it will be inactive.
7///
8/// The [`Outlet`] is aware of how many [`Outlet`]s it is nested within. It will render the content
9/// of the active route that is __exactly as deep__.
10///
11/// # Panic
12/// - When the [`Outlet`] is not nested a [`Link`] component,
13///   but only in debug builds.
14///
15/// # Example
16/// ```rust
17/// # use dioxus::prelude::*;
18/// # use dioxus_router::prelude::*;
19/// #[derive(Clone, Routable)]
20/// #[rustfmt::skip]
21/// enum Route {
22///     #[nest("/wrap")]
23///         #[layout(Wrapper)] // Every layout component must have one Outlet
24///             #[route("/")]
25///             Child {},
26///         #[end_layout]
27///     #[end_nest]
28///     #[route("/")]
29///     Index {},
30/// }
31///
32/// #[component]
33/// fn Index() -> Element {
34///     rsx! {
35///         div {
36///             "Index"
37///         }
38///     }
39/// }
40///
41/// #[component]
42/// fn Wrapper() -> Element {
43///     rsx! {
44///         h1 { "App" }
45///         Outlet::<Route> {} // The content of child routes will be rendered here
46///     }
47/// }
48///
49/// #[component]
50/// fn Child() -> Element {
51///     rsx! {
52///         p {
53///             "Child"
54///         }
55///     }
56/// }
57///
58/// # #[component]
59/// # fn App() -> Element {
60/// #     rsx! {
61/// #         dioxus_router::components::HistoryProvider {
62/// #             history:  move |_| std::rc::Rc::new(dioxus_history::MemoryHistory::with_initial_path(Route::Child {}.to_string())) as std::rc::Rc<dyn dioxus_history::History>,
63/// #             Router::<Route> {}
64/// #         }
65/// #     }
66/// # }
67/// #
68/// # let mut vdom = VirtualDom::new(App);
69/// # vdom.rebuild_in_place();
70/// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><p>Child</p>");
71/// ```
72pub fn Outlet<R: Routable + Clone>() -> Element {
73    OutletContext::<R>::render()
74}