Skip to main content
Version: 0.20

Generic Components

The #[function_component] attribute also works with generic functions for creating generic components.

use std::fmt::Display;
use yew::{function_component, html, Properties, Html};

#[derive(Properties, PartialEq)]
pub struct Props<T>
where
T: PartialEq,
{
data: T,
}

#[function_component]
pub fn MyGenericComponent<T>(props: &Props<T>) -> Html
where
T: PartialEq + Display,
{
html! {
<p>
{ &props.data }
</p>
}
}

// then can be used like this
html! {
<MyGenericComponent<i32> data=123 />
};

// or
html! {
<MyGenericComponent<String> data={"foo".to_string()} />
};