-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Struggling with ways to convert someting into Html
.
#3443
Comments
I added a reproducer. |
The absolute downside of moving away from I think either |
What prevents you from just using I think an implementation: impl<T: Into<Html>> ToHtml for T {} could be helpful. CC: @futursolo |
In the past all All the examples I found in the docs still follow this pattern. If that has changed, the maybe the docs need some update. As well a the pattern of using |
That was changed in the latest release 0.21 and docs were updated to reflect the change. Are there any docs you're seeing that have not been updated? |
I was just looking for them. Could it be that the switch, defaulting to the 0.21 docs , was made a bit after the release? I might have been running into the old docs. |
At least in the module |
Docs default to 0.21 when the release is made. You can see the updated children docs: https://yew.rs/docs/concepts/function-components/children Any type, including Html, can be passed as children. This was also mentioned in the release blog |
I am pretty sure I saw 0.20 docs by default after the release was made.
However, there was nothing in the migration guide: https://yew.rs/docs/migration-guides/yew/from-0_20_0-to-0_21_0 … so there should at least be some "also see the blog post" link in there. |
Hmm, not sure why that is. Could be caching? Also, the migration guide is for breaking changes. I'm not sure where exactly this kind of change would be mentioned. I agree that it be mentioned, just not sure where. Perhaps we could add a section for non breaking changes in the migration guide? Pinging @futursolo as they were the one who made this change. |
I would really call that a breaking change though. What no longer works with { for props.children.iter().map(|item| html!(
<div class="pf-v5-c-accordion__expandable-content-body">
{ item }
</div>
)) } |
Or like this: if !props.children.is_empty() { } |
I don't think this is a breaking change as no existing code breaks. You're allowed to still use ChildrenRenderer if it's needed. You can pass any type as children now. For your case, VList exists (in addition to children). |
Right, but when I need to use Besides, I does break current code, because |
I wonder if we can add blanket trait implantations for this |
Using |
That is what |
Yea, but assume I have something like this:
If I want |
I created #3444 to allow that pattern |
hi, we run into a similar (the same?) issue, we often implemented I played around with implementing a blanket impl block for this, but ran into issues with conflicting implementations (either with the other ToHtml implementations or with the IntoPropValue implementations) so maybe someone who has a better understanding of how they play together can whip something up? |
Hi, the bug was closed, but i think there is still a valid concern here (Please tell if i should open (a) new bug(s). use yew::prelude::*;
struct Print {
text: String,
}
impl Print {
fn new(text: String) -> Self {
Self { text }
}
}
impl From<Print> for Html {
fn from(val: Print) -> Self {
html! {<>{"Hello "}{val.text}</>}
}
}
#[function_component]
fn App() -> Html {
let text = Print::new("World".to_owned());
html! { <div>{text}</div> }
}
fn main() {
yew::Renderer::<App>::new().render();
} Implementing also maybe related is the following issue: use yew::prelude::*;
fn print<T: ToHtml>(text: T) -> Html {
html! {<div>{"Hello "}{text}</div>}
}
#[function_component]
fn App() -> Html {
let vnode = html! {"test"};
print(vnode)
}
fn main() {
yew::Renderer::<App>::new().render();
} here we get the error that |
There are 2 methods on |
I think one of the issues with the new approach is, that one needs to implement many ways now. Depending on the context. Assuming the following for example:
What I want is to just use:
What I need to do is:
So it more work, to get to the same result. |
true but i have to implement a copying/cloning version now, where as before i did not (as @ctron correctly wrote) I'm sorry that i don't have a very deep understanding of the ToHtml/IntoPropValue/ChildRenderer traits to make a more concrete suggestion, but the current way seems a bit unwieldy to use for some use-cases. (currently we work around the issue by requiring 'Clone' on the types we use that for and use impl ToHtml for XXX {
fn to_html(&self) -> Html {
self.clone().into_html()
}
fn into_html(self) -> Html {
self.into()
}
} where |
@futursolo when you left in Children, was there any reason other than backwards compatibility? If not, we can deprecate that and point everyone to use Html or any of its variants as needed@futursolo when you left in Children, was there any reason other than backwards compatibility? If not, we can deprecate that and point everyone to use Html or any of its variants as needed I'm starting to think |
No, not on stable at least, as far as I know. That would need specialization so we can have a blanket impl of |
I have given a try at migrating to help understand the issue here and this is what I found:
@hamza1311 @futursolo if you don't mind I'm re-opening this ticket until OP and others are happy or accept the change of behavior. It looks like something needs to be done: either Yew needs to be updated OR our community see and understand why we change things. (It's not for me, I am not personally much affected by the change of Yew 0.21. The change 1 is a minor annoyance for me, change 2 I think it's actually better to explicitly write down to_string (because it's more explicit) but I don't mind if ToString/Display works again. The change 3 is annoying for me BUT it is unrelated to this issue) [edit: lots of formatting issue in the post haha xD] |
I agree with @cecton here. We should do something to mitigate the issues here We need some blanket implementations that are missing. I'm not sure if we can add them since specialization doesn't exist: impl<T: Into<VNode>> ToHtml for T {} Perhaps even removing ChildrenRenderer outright. I don't see where it's needed. Are there any use cases for it? |
Should we add a method If the VNode is a VList it would be a proxy but if it's anything else it will return (Also we can yield clones of Html with the iterator since VNode will be cheap to clone. But I guess it's best to include that in the #3431 PR) |
I do not think we should be inspecting a VNode. I do not think delivers a very good schematics to users when it comes to some layouts. e.g.: For the following layout, should
|
No, I just didn't want to introduce a breaking changes that breaks every component that has children.
ChildrenRenderer is also used by |
I think that the "is empty" case could also be translated into However, a big thing of Rust (and Yew) is type safety. So when a component requires children of a certain type, that should be supported and natural. And in the past, it was a bit tricky on the implementor's side, but did work great for users. So removing |
I didn't think of vec! [
html_nested!(),
html_nested!()
// ..
] but I believe that can be easily mitigated with the help of a macro |
I think we can also encourage a separation of data and rendered layout. let items = vec![
Item { key: "1", data: "Data 1".to_string() },
Item { key: "2", data: "Data 2".to_string() },
Item { key: "3", data: "Data 3".to_string() },
Item { key: "4", data: "Data 4".to_string() },
];
let render_item = |item: Item<String>| html! { <Comp prop={&item.data} /> };
html! {
<Table<String>
{items}
{render_item}
/>
} In this example, There is a React example: https://mui.com/x/react-data-grid/getting-started/#demo |
Problem
Steps To Reproduce
Steps to reproduce the behavior:
Expected behavior
In the past it was possible to implement
From<Foo> for Html
to use something in thehtml
macro, like this:Now there seem to be two new variants:
ToHtml
, andIntoPropValue<ChildrenRenderer<Vnode>>
. However, it's not always clear when to use what and why sometimesToHtml
, doesn't work.Environment:
v0.21.0.
1.17.1
Questionnaire
The text was updated successfully, but these errors were encountered: