leptos-rs / leptos

Build fast web applications with Rust.
https://leptos.dev
MIT License
15.81k stars 621 forks source link

Path Params not begin passed to nested routes in 0.7 #2719

Closed osmano807 closed 3 weeks ago

osmano807 commented 1 month ago

Describe the bug Path params are passed to the ParentRoute but not the the nested Route. In 0.6 I use this nested routes to implement sidebar/drawers.

Leptos Dependencies Actually running 4bc999e

leptos = { git = "https://github.com/leptos-rs/leptos.git", branch = "leptos_0.7" }
leptos_axum = { git = "https://github.com/leptos-rs/leptos.git", branch = "leptos_0.7" }
leptos_meta = { git = "https://github.com/leptos-rs/leptos.git", branch = "leptos_0.7" }
leptos_router = { git = "https://github.com/leptos-rs/leptos.git", branch = "leptos_0.7" }

To Reproduce

#[component]
pub fn App() -> impl IntoView {
    provide_meta_context();

    view! {
        <Router>
                <Routes fallback=|| { view!{}  }>
                    <ParentRoute path=path!("/test/:test_id") view=TestParent>
                        <Route path=path!("") view=TestChild />
                    </ParentRoute>
                </Routes>
         </Router>
    }
}

#[component]
fn TestParent() -> impl IntoView {
    let params = leptos_router::hooks::use_params_map();

    tracing::info!("TestParent Params: {:?}", params.get());

    view! { <Outlet /> }
}

#[component]
fn TestChild() -> impl IntoView {
    let params = leptos_router::hooks::use_params_map();

    tracing::info!("TestChild Params: {:?}", params.get());

    view! { <p>Child</p> }
}

Expected behavior Should get the :test_id param in both the ParentRoute and child Route, instead I get:

INFO request:call: app/src/lib.rs:264: TestParent Params: ParamsMap([("test_id", "my_id")]) method=GET uri=/test/my_id version=HTTP/1.1
INFO request:call: app/src/lib.rs:273: TestChild Params: ParamsMap([]) method=GET uri=/test/my_id version=HTTP/1.1
gbj commented 1 month ago

Oh interesting, I hadn't noticed that this is now backwards: putting the param in the child gives it to both of them.

#[component]
pub fn App() -> impl IntoView {
    provide_meta_context();

    view! {
        <Router>
            <Routes fallback=|| ()>
                <ParentRoute path=path!("/test") view=TestParent>
                    <Route path=path!(":test_id") view=TestChild/>
                </ParentRoute>
            </Routes>
        </Router>
    }
}
TestParent Params: ParamsMap([("test_id", "foo")])
TestChild Params: ParamsMap([("test_id", "foo")])

Will fix. Honestly though maybe just providing all the matched params to routes at every level makes the most sense. This has been raised a couple times and is reasonable enough.