Generic Components

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

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

4. #[derive(Properties, PartialEq)]
5. pub struct Props<T>
6. where
7. T: PartialEq,
8. {
9. data: T,
10. }

12. #[function_component]
13. pub fn MyGenericComponent<T>(props: &Props<T>) -> Html
14. where
15. T: PartialEq + Display,
16. {
17. html! {
18. <p>
19. { &props.data }
20. </p>
21. }
22. }

24. // then can be used like this
25. html! {
26. <MyGenericComponent<i32> data=123 />
27. };

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