1
0

outlet.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. use crate::{contexts::outlet::OutletContext, routable::Routable};
  2. use dioxus::prelude::*;
  3. /// An outlet for the current content.
  4. ///
  5. /// Only works as descendant of a [`GenericRouter`] component, otherwise it will be inactive.
  6. ///
  7. /// The [`Outlet`] is aware of how many [`Outlet`]s it is nested within. It will render the content
  8. /// of the active route that is __exactly as deep__.
  9. ///
  10. /// # Panic
  11. /// - When the [`Outlet`] is not nested within another component calling the [`use_router`] hook,
  12. /// but only in debug builds.
  13. ///
  14. /// # Example
  15. /// ```rust
  16. /// # use dioxus::prelude::*;
  17. /// # use serde::{Deserialize, Serialize};
  18. /// # use dioxus_router::prelude::*;
  19. /// #[derive(Clone, Serialize, Deserialize, 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. /// #[inline_props]
  33. /// fn Index(cx: Scope) -> Element {
  34. /// render! {
  35. /// div {
  36. /// "Index"
  37. /// }
  38. /// }
  39. /// }
  40. ///
  41. /// #[inline_props]
  42. /// fn Wrapper(cx: Scope) -> Element {
  43. /// render! {
  44. /// h1 { "App" }
  45. /// Outlet {} // The content of child routes will be rendered here
  46. /// }
  47. /// }
  48. ///
  49. /// #[inline_props]
  50. /// fn Child(cx: Scope) -> Element {
  51. /// render! {
  52. /// p {
  53. /// "Child"
  54. /// }
  55. /// }
  56. /// }
  57. ///
  58. /// # fn App(cx: Scope) -> Element {
  59. /// # render! {
  60. /// # Router {
  61. /// # config: RouterConfiguration {
  62. /// # history: Box::new(MemoryHistory::with_initial_path("/wrap").unwrap()),
  63. /// # ..Default::default()
  64. /// # }
  65. /// # }
  66. /// # }
  67. /// # }
  68. /// #
  69. /// # let mut vdom = VirtualDom::new(App);
  70. /// # let _ = vdom.rebuild();
  71. /// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><p>Child</p>");
  72. /// ```
  73. pub fn GenericOutlet<R: Routable + Clone>(cx: Scope) -> Element {
  74. OutletContext::render::<R>(cx)
  75. }