Muhammad Hamza 0eb167ac78
Remove ToHtml trait (#3453)
* remove ToHtml trait

* re-add display impls

* make Vec::clone expilit

* fix doc

* fix conflicting impls

Into<Html> and Display can't be implemented on the same type

* update docs

* blanket impl won't work here

* bring back `Vec<VNode>: IntoPropValue<VNode>`

* macro tests

* Revert "fix conflicting impls"

This reverts commit 52f3c1fa8174489ba9cc783d708a49cc7b9c90a4.
These impls are fine now

* make examples compile

* .clone() should be before .into()

* Rc VList

* Make use of ImplicitClone and AttrValue in example

(There is more work to do but it's complicated so I will do it in
another PR)

* Impl ImplicitClone on VChild

---------

Co-authored-by: Cecile Tonglet <cecile.tonglet@cecton.com>
2023-10-28 16:11:28 +05:00

45 lines
821 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 + Clone + Into<Html>,
{
html! {
<p>
{ props.data.clone().into() }
</p>
}
}
// then can be used like this
html! {
<MyGenericComponent<i32> data=123 />
};
// or
html! {
<MyGenericComponent<String> data={"foo".to_string()} />
};
```