fix: typed route params with #[derive(Params)] (#488)

This commit is contained in:
Greg Johnston 2023-02-07 17:28:46 -05:00 committed by GitHub
parent 086326324e
commit 8a1adaefaf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 10 deletions

View file

@ -86,21 +86,19 @@ pub fn ContactList(cx: Scope) -> impl IntoView {
} }
} }
#[derive(Params, PartialEq, Clone, Debug)]
pub struct ContactParams {
id: usize,
}
#[component] #[component]
pub fn Contact(cx: Scope) -> impl IntoView { pub fn Contact(cx: Scope) -> impl IntoView {
log::debug!("rendering <Contact/>"); log::debug!("rendering <Contact/>");
let params = use_params_map(cx); let params = use_params::<ContactParams>(cx);
let contact = create_resource( let contact = create_resource(
cx, cx,
move || { move || params().map(|params| params.id).ok(),
params()
.get("id")
.cloned()
.unwrap_or_default()
.parse::<usize>()
.ok()
},
// any of the following would work (they're identical) // any of the following would work (they're identical)
// move |id| async move { get_contact(id).await } // move |id| async move { get_contact(id).await }
// move |id| get_contact(id), // move |id| get_contact(id),

View file

@ -29,7 +29,7 @@ pub fn impl_params(ast: &syn::DeriveInput) -> proc_macro::TokenStream {
let gen = quote! { let gen = quote! {
impl Params for #name { impl Params for #name {
fn from_map(map: &::leptos_router::ParamsMap) -> Result<Self, ::leptos_router::RouterError> { fn from_map(map: &::leptos_router::ParamsMap) -> Result<Self, ::leptos_router::ParamsError> {
Ok(Self { Ok(Self {
#(#fields,)* #(#fields,)*
}) })