fix nested routers
This commit is contained in:
1 parent
8d166cf40d
commit
128f3e4425
3 files changed
+27
-14
No files matched your search
@@ -41,10 +41,8 @@ fn app(cx: Scope<AppProps>) -> Element {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[server(PostServerData)]
|
#[server(PostServerData)]
|
||||||
async fn post_server_data(
|
async fn post_server_data(data: String) -> Result<(), ServerFnError> {
|
||||||
#[extract] axum::extract::Host(host): axum::extract::Host,
|
let axum::extract::Host(host): axum::extract::Host = extract()?;
|
||||||
data: String,
|
|
||||||
) -> Result<(), ServerFnError> {
|
|
||||||
println!("Server received: {}", data);
|
println!("Server received: {}", data);
|
||||||
println!("{:?}", host);
|
println!("{:?}", host);
|
||||||
|
|
||||||
|
|||||||
@@ -68,5 +68,5 @@ use dioxus::prelude::*;
|
|||||||
/// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><p>Child</p>");
|
/// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><p>Child</p>");
|
||||||
/// ```
|
/// ```
|
||||||
pub fn GenericOutlet<R: Routable + Clone>(cx: Scope) -> Element {
|
pub fn GenericOutlet<R: Routable + Clone>(cx: Scope) -> Element {
|
||||||
OutletContext::render::<R>(cx)
|
OutletContext::<R>::render(cx)
|
||||||
}
|
}
|
||||||
@@ -2,29 +2,44 @@ use dioxus::prelude::*;
|
|||||||
|
|
||||||
use crate::{routable::Routable, utils::use_router_internal::use_router_internal};
|
use crate::{routable::Routable, utils::use_router_internal::use_router_internal};
|
||||||
|
|
||||||
#[derive(Clone)]
|
pub(crate) struct OutletContext<R> {
|
||||||
pub(crate) struct OutletContext {
|
|
||||||
pub current_level: usize,
|
pub current_level: usize,
|
||||||
|
_marker: std::marker::PhantomData<R>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn use_outlet_context(cx: &ScopeState) -> &OutletContext {
|
impl<R> Clone for OutletContext<R> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
OutletContext {
|
||||||
|
current_level: self.current_level,
|
||||||
|
_marker: std::marker::PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn use_outlet_context<R: 'static>(cx: &ScopeState) -> &OutletContext<R> {
|
||||||
let outlet_context = cx.use_hook(|| {
|
let outlet_context = cx.use_hook(|| {
|
||||||
cx.consume_context()
|
cx.consume_context().unwrap_or(OutletContext::<R> {
|
||||||
.unwrap_or(OutletContext { current_level: 0 })
|
current_level: 0,
|
||||||
|
_marker: std::marker::PhantomData,
|
||||||
|
})
|
||||||
});
|
});
|
||||||
outlet_context
|
outlet_context
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OutletContext {
|
impl<R> OutletContext<R> {
|
||||||
pub(crate) fn render<R: Routable + Clone>(cx: Scope) -> Element<'_> {
|
pub(crate) fn render(cx: Scope) -> Element<'_>
|
||||||
|
where
|
||||||
|
R: Routable + Clone,
|
||||||
|
{
|
||||||
let router = use_router_internal::<R>(cx)
|
let router = use_router_internal::<R>(cx)
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.expect("Outlet must be inside of a router");
|
.expect("Outlet must be inside of a router");
|
||||||
let outlet = use_outlet_context(cx);
|
let outlet: &OutletContext<R> = use_outlet_context(cx);
|
||||||
let current_level = outlet.current_level;
|
let current_level = outlet.current_level;
|
||||||
cx.provide_context({
|
cx.provide_context({
|
||||||
OutletContext {
|
OutletContext::<R> {
|
||||||
current_level: current_level + 1,
|
current_level: current_level + 1,
|
||||||
|
_marker: std::marker::PhantomData,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user