-
-
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
Why are Component and Renderable separate traits? #339
Comments
While not a definitive answer, I think I can provide a reasonable guess. I don't believe that the impl Renderable<ComponentStruct> for ComponentStruct {
fn view(&self) -> Html<ComponentStruct> {
html! {
<div>
{self.stateless_struct}
</div>
}
}
}
struct StatelessStruct {
pub thing: String
}
impl <T> Renderable<T: Component> for StatelessStruct {
fn view(&self) -> Html<T> {
// whatever, as long as it never needs to set up callbacks that aren't present in the struct itself in relation to T
}
} I personally would prefer if Component has its own impl <T> Renderable<T> for T where T: Component {
fn render(&self) -> Html<T> {
self.view()
}
} But this approach would incur an additional small performance penalty (extra function call) or binary size bloat (if it was inlined), and also impacts discoverability of the concept of encapsulating a pure rendering function within the domain of a specific struct/enum. The |
The Halogen framework I mentioned (for the purely functional PureScript language) is doing exactly that. When creating a component, you provide an And in that framework, both functions are part of each "component" so I think the reason that yew aims to do the same is no argument against having a
It would inline this call and it wouldn't cause bloat compared to the current solution because each component's
How so? :)
The design of these 2 traits doesn't change the number of times the Btw, I would prefer if both traits' view methods were also called the same ( |
I agree with all of your points, I think I was glossing over these details when responding initially. To clarify my prior point about discoverability, I mostly make that within the current context regarding yew's current state of documentation, where there is no guide, and the last docs.rs entry that compiled properly is from 0.2.0. Eventually, that situation will improve, but I think by separating the traits, it does make it pretty clear that non-components can be rendered as well, which for beginners is helpful in avoiding the "everything rendered on page must be a component" anti-pattern. |
Yea the docs definitely need to be improved, so that they build etc. Btw, could you think of a reason/usecase why you'd want to have a component that's not If not, I think the most ergonomic way (for yew users) would be to have a Also, I wouldn't even mind if everything that occurs inside
I don't think it would be an anti-pattern. It would streamline/unify the trait design. |
From https://bluejekyll.github.io/blog/rust/2018/07/22/static-web-app-rust.html
I've been wondering why this is, too! Does someone have an answer? I'd prefer if they get merged into one trait.
In other frameworks (e.g. Halogen (PureScript)), every component has a
render
/view
andeval
/update
function.The text was updated successfully, but these errors were encountered: