mirror of
https://github.com/leptos-rs/leptos
synced 2024-09-20 14:32:00 +00:00
fix: typed route params with #[derive(Params)]
(#488)
This commit is contained in:
parent
086326324e
commit
8a1adaefaf
2 changed files with 8 additions and 10 deletions
|
@ -86,21 +86,19 @@ pub fn ContactList(cx: Scope) -> impl IntoView {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Params, PartialEq, Clone, Debug)]
|
||||
pub struct ContactParams {
|
||||
id: usize,
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn Contact(cx: Scope) -> impl IntoView {
|
||||
log::debug!("rendering <Contact/>");
|
||||
|
||||
let params = use_params_map(cx);
|
||||
let params = use_params::<ContactParams>(cx);
|
||||
let contact = create_resource(
|
||||
cx,
|
||||
move || {
|
||||
params()
|
||||
.get("id")
|
||||
.cloned()
|
||||
.unwrap_or_default()
|
||||
.parse::<usize>()
|
||||
.ok()
|
||||
},
|
||||
move || params().map(|params| params.id).ok(),
|
||||
// any of the following would work (they're identical)
|
||||
// move |id| async move { get_contact(id).await }
|
||||
// move |id| get_contact(id),
|
||||
|
|
|
@ -29,7 +29,7 @@ pub fn impl_params(ast: &syn::DeriveInput) -> proc_macro::TokenStream {
|
|||
|
||||
let gen = quote! {
|
||||
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 {
|
||||
#(#fields,)*
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue