dioxus_router/components/
outlet.rs

1use crate::{outlet::OutletContext, *};
2use dioxus_core::Element;
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/// #[derive(Clone, Routable)]
19/// #[rustfmt::skip]
20/// enum Route {
21///     #[nest("/wrap")]
22///         #[layout(Wrapper)] // Every layout component must have one Outlet
23///             #[route("/")]
24///             Child {},
25///         #[end_layout]
26///     #[end_nest]
27///     #[route("/")]
28///     Index {},
29/// }
30///
31/// #[component]
32/// fn Index() -> Element {
33///     rsx! {
34///         div {
35///             "Index"
36///         }
37///     }
38/// }
39///
40/// #[component]
41/// fn Wrapper() -> Element {
42///     rsx! {
43///         h1 { "App" }
44///         Outlet::<Route> {} // The content of child routes will be rendered here
45///     }
46/// }
47///
48/// #[component]
49/// fn Child() -> Element {
50///     rsx! {
51///         p {
52///             "Child"
53///         }
54///     }
55/// }
56///
57/// # #[component]
58/// # fn App() -> Element {
59/// #     rsx! {
60/// #         dioxus_router::components::HistoryProvider {
61/// #             history:  move |_| std::rc::Rc::new(dioxus_history::MemoryHistory::with_initial_path(Route::Child {}.to_string())) as std::rc::Rc<dyn dioxus_history::History>,
62/// #             Router::<Route> {}
63/// #         }
64/// #     }
65/// # }
66/// #
67/// # let mut vdom = VirtualDom::new(App);
68/// # vdom.rebuild_in_place();
69/// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><p>Child</p>");
70/// ```
71pub fn Outlet<R: Routable + Clone>() -> Element {
72    OutletContext::<R>::render()
73}