Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern binding for auto-props #2

Open
kirillsemyonkin opened this issue May 23, 2023 · 0 comments
Open

Pattern binding for auto-props #2

kirillsemyonkin opened this issue May 23, 2023 · 0 comments

Comments

@kirillsemyonkin
Copy link
Collaborator

As of writing, yew-autoprops does not support pattern-binding the props using the built-in @ operator (syntactically correct without the #[autoprops_component]):

#[derive(PartialEq)]
pub struct ExampleProp {
    i: u64,
}

#[autoprops_component]
pub fn Example(example_prop @ ExampleProp { i }: &ExampleProp) -> Html {
    html! {
    }
}

#[function_component]
pub fn App() -> Html {
    html! {
        <Example example_prop={ ExampleProp { i: 0 } } />
    }
}

Errors:

expected `:`, found `@`
the trait bound `ExampleProps: Properties` is not satisfied
no field `example_prop` on type `ExampleProps`

Possible Solution

Handle pattern after the @ operator.

#[derive(PartialEq)]
pub struct ExampleProp {
    i: u64,
}

#[derive(PartialEq, Properties)]
pub struct ExampleProps {
    example_prop: ExampleProp,
}

#[function_component]
pub fn Example(
    ExampleProps {
        // without the `example_prop @` the `example_prop` name will not be accessible in the function body
        example_prop: example_prop @ ExampleProp { i }, 
    }: &ExampleProps,
) -> Html {
    html! {
    }
}

#[function_component]
pub fn App() -> Html {
    html! {
        <Example example_prop={ ExampleProp { i: 0 } } />
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant