--- title: 'Components' description: 'Create complex layouts with component hierarchies' --- ## Basic Components can be used in the `html!` macro: ```rust use yew::prelude::*; #[component] fn MyComponent() -> Html { html! { { "This component has no properties!" } } } #[derive(Clone, PartialEq, Properties)] struct Props { user_first_name: String, user_last_name: String, } #[component] fn MyComponentWithProps(props: &Props) -> Html { let Props { user_first_name, user_last_name } = props; html! { <>{"user_first_name: "}{user_first_name}{" and user_last_name: "}{user_last_name} } } let props = Props { user_first_name: "Bob".to_owned(), user_last_name: "Smith".to_owned(), }; html!{ <> // No properties // With Properties // With the whole set of props provided at once // With Properties from a variable and specific values overridden }; ``` ## Nested Components can accept child components/elements if they have a `children` field in their `Properties` ```rust title="parent.rs" use yew::prelude::*; #[derive(PartialEq, Properties)] struct Props { id: String, children: Html, } #[component] fn Container(props: &Props) -> Html { html! {
{ props.children.clone() }
} } html! {

{ "Hi" }

{ "Hello" }
}; ``` 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](https://doc.rust-lang.org/stable/reference/expressions/struct-expr.html#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. ```rust use yew::prelude::*; #[derive(PartialEq, Properties)] struct Props { id: String, children: Html, } #[component] fn Container(props: &Props) -> Html { html! {
{ props.children.clone() }
} } let props = yew::props!(Props { id: "container-2", children: Html::default(), }); html! { // props.children will be overwritten with this { "I am a child, as you can see" } }; ``` ## Relevant examples - [Function Todo MVC](https://github.com/yewstack/yew/tree/master/examples/function_todomvc) - [Function Router](https://github.com/yewstack/yew/tree/master/examples/function_router)