Skip to content

Pattern binding for auto-props #2

@kirillsemyonkin

Description

@kirillsemyonkin

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 } } />
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions