mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
* Partially copy useful implementation. * Adjust conversion. * Temporary fix iterator. * Add ToString implementation. * Add Renderable trait. * Make Macro tests pass. * Add tests for render_prop as Children. * Update benchmark and Children used in yew packages. * Selective suppress lints. * Rollback unintentional rollback. * Fix rustfmt. * Remove unneeded implementation. * Update Comment. * Rollback more changes. * Rollback more changes. * Fix website. * Fix documentation tests. * Add prelude. * Fix test. * Blanket Implementation for &'_ T for Renderable types. * Implement Renderable for &str. * Update signature. * Update children to Html in examples. * Remove unnecessary dereferencing. * Rollback nested_list example. * Fix comment. * Convert to Pattern Matching. * Add tracing issue. * Rename Renderable to ToHtml. * Move ToHtml to yew::html. * Convert more to match pattern.
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()} />
|
|
};
|
|
```
|