Macro yew::props

source ·
props!() { /* proc-macro */ }
Expand description

Build Properties outside of the html! macro.

It’s already possible to create properties like normal Rust structs but if there are lots of optional props the end result is often needlessly verbose. This macro allows you to build properties the same way the html! macro does.

The macro doesn’t support special props like ref and key, they need to be set in the html! macro.

You can read more about Properties in the Yew Docs.

Example

use std::borrow::Cow;

#[derive(Clone, Properties, PartialEq)]
struct Props {
    #[prop_or_default]
    id: usize,
    name: Cow<'static, str>,
}

struct MyComponent(Props);
impl Component for MyComponent {
    type Properties = Props;
    // ...
}

// You can build props directly ...
let props = yew::props!(Props {
    name: Cow::from("Minka")
});
// ... or build the associated properties of a component
let props = yew::props!(MyComponent::Properties {
    id: 2,
    name: Cow::from("Lemmy")
});

// Use the Rust-like struct update syntax to create a component with the props.
html! {
    <MyComponent key=1 ..props />
}