Components

Basic

Any type that implements Component can be used in the html! macro:

  1. use yew::{Component, Html, html, Context, Properties};
  2. struct MyComponent;
  3. impl Component for MyComponent {
  4. type Message = ();
  5. type Properties = ();
  6. fn create(_ctx: &Context<Self>) -> Self {
  7. Self
  8. }
  9. fn view(&self, _ctx: &Context<Self>) -> Html {
  10. html! {
  11. { "This component has no properties!" }
  12. }
  13. }
  14. }
  15. #[derive(Clone, PartialEq, Properties)]
  16. struct Props {
  17. prop1: String,
  18. prop2: String,
  19. }
  20. struct MyComponentWithProps;
  21. impl Component for MyComponentWithProps {
  22. type Message = ();
  23. type Properties = Props;
  24. fn create(_ctx: &Context<Self>) -> Self {
  25. Self
  26. }
  27. fn view(&self, ctx: &Context<Self>) -> Html {
  28. html! {
  29. {
  30. format!(
  31. "prop1: {} and prop2: {}",
  32. ctx.props().prop1,
  33. ctx.props().prop2
  34. )
  35. }
  36. }
  37. }
  38. }
  39. let props = Props {
  40. prop1: "Hello".to_owned(),
  41. prop2: "World".to_owned(),
  42. };
  43. html!{
  44. <>
  45. // No properties
  46. <MyComponent />
  47. // With Properties
  48. <MyComponentWithProps prop1="lorem" prop2="ipsum" />
  49. // With the whole set of props provided at once
  50. <MyComponentWithProps ..props.clone() />
  51. // With Properties from a variable and specific values overridden
  52. <MyComponentWithProps prop2="lorem" ..props />
  53. </>
  54. };

Nested

Components can be passed children if they have a children field in their Properties.

parent.rs

  1. use yew::{Children, Component, Context, html, Html, Properties};
  2. #[derive(PartialEq, Properties)]
  3. struct Props {
  4. id: String,
  5. children: Children,
  6. }
  7. struct Container;
  8. impl Component for Container {
  9. type Message = ();
  10. type Properties = Props;
  11. fn create(_ctx: &Context<Self>) -> Self {
  12. Self
  13. }
  14. fn view(&self, ctx: &Context<Self>) -> Html {
  15. html! {
  16. <div id={ctx.props().id.clone()}>
  17. { ctx.props().children.clone() }
  18. </div>
  19. }
  20. }
  21. }
  22. html! {
  23. <Container id="container">
  24. <h4>{ "Hi" }</h4>
  25. <div>{ "Hello" }</div>
  26. </Container>
  27. };

The html! macro allows you to pass a base expression with the ..props syntax instead of specifying each property individually, similar to Rust’s Functional Update Syntax. This base expression must occur after any individual props are passed. When passing a base props expression with a children field, the children passed in the html! macro overwrite the ones already present in the props.

  1. use yew::{Children, Component, Context, html, Html, props, Properties};
  2. #[derive(PartialEq, Properties)]
  3. struct Props {
  4. id: String,
  5. children: Children,
  6. }
  7. struct Container;
  8. impl Component for Container {
  9. type Message = ();
  10. type Properties = Props;
  11. fn create(_ctx: &Context<Self>) -> Self {
  12. Self
  13. }
  14. fn view(&self, ctx: &Context<Self>) -> Html {
  15. html! {
  16. <div id={ctx.props().id.clone()}>
  17. { ctx.props().children.clone() }
  18. </div>
  19. }
  20. }
  21. }
  22. let props = yew::props!(Container::Properties {
  23. id: "container-2",
  24. children: Children::default(),
  25. });
  26. html! {
  27. <Container ..props>
  28. // props.children will be overwritten with this
  29. <span>{ "I am a child, as you can see" }</span>
  30. </Container>
  31. };

Nested Children with Props

Nested component properties can be accessed and mutated if the containing component types its children. In the following example, the List component can wrap ListItem components. For a real world example of this pattern, check out the yew-router source code. For a more advanced example, check out the nested-list example in the main yew repository.

  1. use std::rc::Rc;
  2. use yew::{html, ChildrenWithProps, Component, Context, Html, Properties};
  3. #[derive(Clone, PartialEq, Properties)]
  4. pub struct ListItemProps {
  5. value: String,
  6. }
  7. pub struct ListItem;
  8. impl Component for ListItem {
  9. type Message = ();
  10. type Properties = ListItemProps;
  11. fn create(_ctx: &Context<Self>) -> Self {
  12. Self
  13. }
  14. fn view(&self, ctx: &Context<Self>) -> Html {
  15. html! {
  16. <span>
  17. { ctx.props().value.clone() }
  18. </span>
  19. }
  20. }
  21. }
  22. #[derive(PartialEq, Properties)]
  23. pub struct Props {
  24. pub children: ChildrenWithProps<ListItem>,
  25. }
  26. pub struct List;
  27. impl Component for List {
  28. type Message = ();
  29. type Properties = Props;
  30. fn create(_ctx: &Context<Self>) -> Self {
  31. Self
  32. }
  33. fn view(&self, ctx: &Context<Self>) -> Html {
  34. html! {{
  35. for ctx.props().children.iter().map(|mut item| {
  36. let mut props = Rc::make_mut(&mut item.props);
  37. props.value = format!("item-{}", props.value);
  38. item
  39. })
  40. }}
  41. }
  42. }
  43. html! {
  44. <List>
  45. <ListItem value="a" />
  46. <ListItem value="b" />
  47. <ListItem value="c" />
  48. </List>
  49. };

Relevant examples