mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
* 0.20 Changelog * Improve changelog generator * Add blog post * Add blog post * Apply suggestions from code review Co-authored-by: WorldSEnder <WorldSEnder@users.noreply.github.com> Co-authored-by: Julius Lungys <32368314+voidpumpkin@users.noreply.github.com> * update Changelog * update Cargo.toml * changelog gen compiles * website version 0.20 * add migration guides * prettier * i18n Co-authored-by: WorldSEnder <WorldSEnder@users.noreply.github.com> Co-authored-by: Julius Lungys <32368314+voidpumpkin@users.noreply.github.com>
45 lines
796 B
Plaintext
45 lines
796 B
Plaintext
---
|
|
title: 'Generic Components'
|
|
description: 'The #[function_component] attribute'
|
|
---
|
|
|
|
import Tabs from '@theme/Tabs'
|
|
import TabItem from '@theme/TabItem'
|
|
|
|
The `#[function_component]` attribute also works with generic functions for creating generic components.
|
|
|
|
```rust
|
|
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()} />
|
|
};
|
|
```
|