mirror of
https://github.com/yewstack/yew.git
synced 2026-01-25 16:43:15 +00:00
* Update CHANGELOG Changelog run: https://github.com/yewstack/yew/actions/runs/6283917852/job/17064800916 * docusaurus docs:version 0.21 * migration guide * blog post * prettier * make website warnings go away * make GA work * Apply suggestions from code review * Apply suggestion from review Co-authored-by: Kaede Hoshikawa <futursolo@users.noreply.github.com> * prettier --------- Co-authored-by: Kaede Hoshikawa <futursolo@users.noreply.github.com>
45 lines
803 B
Plaintext
45 lines
803 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, ToHtml};
|
|
|
|
#[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 + ToHtml,
|
|
{
|
|
html! {
|
|
<p>
|
|
{ &props.data }
|
|
</p>
|
|
}
|
|
}
|
|
|
|
// then can be used like this
|
|
html! {
|
|
<MyGenericComponent<i32> data=123 />
|
|
};
|
|
|
|
// or
|
|
html! {
|
|
<MyGenericComponent<String> data={"foo".to_string()} />
|
|
};
|
|
```
|