Properties

Properties enable child and parent components to communicate with each other. Every component has an associated properties type which describes what is passed down from the parent. In theory this can be any type that implements the Properties trait, but in practice there’s no reason for it to be anything but a struct where each field represents a property.

Derive macro

Instead of implementing the Properties trait yourself, you should use #[derive(Properties)] to automatically generate the implementation instead. Types for which you derive Properties must also implement PartialEq.

Field attributes

When deriving Properties, all fields are required by default. The following attributes allow you to give your props initial values which will be used unless they’re set to another value.

Properties - 图1tip

Attributes aren’t visible in Rustdoc generated documentation. The doc strings of your properties should mention whether a prop is optional and if it has a special default value.

#[prop_or_default]

Initialize the prop value with the default value of the field’s type using the Default trait.

#[prop_or(value)]

Use value to initialize the prop value. value can be any expression that returns the field’s type. For example, to default a boolean prop to true, use the attribute #[prop_or(true)].

#[prop_or_else(function)]

Call function to initialize the prop value. function should have the signature FnMut() -> T where T is the field type.

PartialEq

Properties require PartialEq to be implemented. This is so that they can be compared by Yew to call the changed method only when they change.

Memory/speed overhead of using Properties

Internally properties are reference counted. This means that only a pointer is passed down the component tree for props. It saves us from the cost of having to clone the entire props, which might be expensive.

Properties - 图2tip

Make use of AttrValue which is our custom type for attribute values instead of defining them as String or another similar type.

Example

  1. use yew::Properties;
  2. /// Importing the AttrValue from virtual_dom
  3. use yew::virtual_dom::AttrValue;
  4. #[derive(Clone, PartialEq)]
  5. pub enum LinkColor {
  6. Blue,
  7. Red,
  8. Green,
  9. Black,
  10. Purple,
  11. }
  12. fn create_default_link_color() -> LinkColor {
  13. LinkColor::Blue
  14. }
  15. #[derive(Properties, PartialEq)]
  16. pub struct LinkProps {
  17. /// The link must have a target.
  18. href: AttrValue,
  19. /// Also notice that we're using AttrValue instead of String
  20. text: AttrValue,
  21. /// Color of the link. Defaults to `Blue`.
  22. #[prop_or_else(create_default_link_color)]
  23. color: LinkColor,
  24. /// The view function will not specify a size if this is None.
  25. #[prop_or_default]
  26. size: Option<u32>,
  27. /// When the view function doesn't specify active, it defaults to true.
  28. #[prop_or(true)]
  29. active: bool,
  30. }

Props macro

The yew::props! macro allows you to build properties the same way the html! macro does it.

The macro uses the same syntax as a struct expression except that you can’t use attributes or a base expression (Foo { ..base }). The type path can either point to the props directly (path::to::Props) or the associated properties of a component (MyComp::Properties).

  1. use yew::{props, Properties, virtual_dom::AttrValue};
  2. #[derive(Clone, PartialEq)]
  3. pub enum LinkColor {
  4. Blue,
  5. Red,
  6. Green,
  7. Black,
  8. Purple,
  9. }
  10. fn create_default_link_color() -> LinkColor {
  11. LinkColor::Blue
  12. }
  13. #[derive(Properties, PartialEq)]
  14. pub struct LinkProps {
  15. /// The link must have a target.
  16. href: AttrValue,
  17. /// Also notice that we're using AttrValue instead of String
  18. text: AttrValue,
  19. /// Color of the link. Defaults to `Blue`.
  20. #[prop_or_else(create_default_link_color)]
  21. color: LinkColor,
  22. /// The view function will not specify a size if this is None.
  23. #[prop_or_default]
  24. size: Option<u32>,
  25. /// When the view function doesn't specify active, it defaults to true.
  26. #[prop_or(true)]
  27. active: bool,
  28. }
  29. impl LinkProps {
  30. /// Notice that this function receives href and text as String
  31. /// We can use `AttrValue::from` to convert it to a `AttrValue`
  32. pub fn new_link_with_size(href: String, text: String, size: u32) -> Self {
  33. props! {LinkProps {
  34. href: AttrValue::from(href),
  35. text: AttrValue::from(text),
  36. size,
  37. }}
  38. }
  39. }