Skip to content

Commit 702fbac

Browse files
authored
Fix layouts in child routers (#4994)
* fix layouts in child routers * test child route layouts
1 parent 5dc22f8 commit 702fbac

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

packages/router/src/components/child_router.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// Components that allow the macro to add child routers. This component provides a context
22
/// to the child router that maps child routes to root routes and vice versa.
3-
use crate::Routable;
3+
use crate::{Outlet, OutletContext, Routable};
44
use dioxus_core::{provide_context, try_consume_context, use_hook, Element};
5-
use dioxus_core_macro::{component, Props};
5+
use dioxus_core_macro::{component, rsx, Props};
66

77
/// Maps a child route into the root router and vice versa
88
// NOTE: Currently child routers only support simple static prefixes, but this
@@ -61,8 +61,9 @@ pub fn ChildRouter<R: Routable>(props: ChildRouterProps<R>) -> Element {
6161
provide_context(ChildRouteMapping {
6262
format_route_as_root_route: props.format_route_as_root_route,
6363
parse_route_from_root_route: props.parse_route_from_root_route,
64-
})
64+
});
65+
provide_context(OutletContext::<R>::new());
6566
});
6667

67-
props.route.render(0)
68+
rsx! { Outlet::<R> {} }
6869
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#![allow(unused)]
2+
3+
use std::rc::Rc;
4+
5+
use dioxus::prelude::*;
6+
use dioxus_history::{History, MemoryHistory};
7+
use dioxus_router::components::HistoryProvider;
8+
9+
fn prepare(path: impl Into<String>) -> VirtualDom {
10+
let mut vdom = VirtualDom::new_with_props(
11+
App,
12+
AppProps {
13+
path: path.into().parse().unwrap(),
14+
},
15+
);
16+
vdom.rebuild_in_place();
17+
return vdom;
18+
19+
#[derive(Routable, Clone, PartialEq)]
20+
#[rustfmt::skip]
21+
enum Route {
22+
#[layout(Layout)]
23+
#[child("/")]
24+
Child { child: ChildRoute },
25+
}
26+
27+
#[derive(Routable, Clone, PartialEq)]
28+
#[rustfmt::skip]
29+
enum ChildRoute{
30+
#[layout(ChildLayout)]
31+
#[route("/")]
32+
RootIndex {}
33+
}
34+
35+
#[component]
36+
fn App(path: Route) -> Element {
37+
rsx! {
38+
h1 { "App" }
39+
HistoryProvider {
40+
history: move |_| Rc::new(MemoryHistory::with_initial_path(path.clone())) as Rc<dyn History>,
41+
Router::<Route> {}
42+
}
43+
}
44+
}
45+
46+
#[component]
47+
fn RootIndex() -> Element {
48+
rsx! { h2 { "Root Index" } }
49+
}
50+
51+
#[component]
52+
fn Layout() -> Element {
53+
rsx! {
54+
h2 { "parent layout" }
55+
Outlet::<Route> { }
56+
}
57+
}
58+
59+
#[component]
60+
fn ChildLayout() -> Element {
61+
rsx! {
62+
h2 { "child layout" }
63+
Outlet::<ChildRoute> { }
64+
}
65+
}
66+
}
67+
68+
#[test]
69+
fn root_index() {
70+
let vdom = prepare("/");
71+
let html = dioxus_ssr::render(&vdom);
72+
73+
assert_eq!(
74+
html,
75+
"<h1>App</h1><h2>parent layout</h2><h2>child layout</h2><h2>Root Index</h2>"
76+
);
77+
}

packages/router/tests/via_ssr/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod child_outlet;
12
mod link;
23
mod navigation;
34
mod outlet;

0 commit comments

Comments
 (0)