Yew: Confusing error message for component with children when no children are supplied

Created on 22 Sep 2020  路  2Comments  路  Source: yewstack/yew

I have a component with

struct Props {
    children: Children,
}

and when using the component with no children:

<MyComponent>
</MyComponent>

I get the following error message:

no method named `build` found for struct `my_component::PropsBuilder<my_component:PropsBuilderStep_missing_required_prop_children>` in the current scope
method not found in `my_component::PropsBuilder<my_component::PropsBuilderStep_missing_required_prop_children>`rustc(E0599)
my_component.rs(8, 17): method `build` not found for this
lib.rs(101, 1): Error originated from macro here

Expected behavior
I would expect that the component should work without children supplied, or at least provide a more informative error message.

Environment:

  • Yew version: v0.17
  • Rust version: 1.47.0-beta.3
  • Target, if relevant: wasm32-unknown-unknown
  • Web library: web-sys

Questionnaire

  • [ ] I'm interested in fixing this myself but don't know where to start
  • [ ] I would like to fix and I have a solution
  • [x] I don't have time to fix this right now, but maybe later
bug macro

Most helpful comment

This is a hard problem to solve.
The biggest problem here is that the html! macro has no knowledge of the Properties struct.
A consequence of this is that we have no idea whether the component has a children field or not.
In return, this means that we cannot simply pass an empty Children because that would break props without a children field.

Improving the error message is also, for the same reasons, a daunting task.

There's still hope though. We can turn children into a special case in the Properties derive macro and use setter methods to set it.
A method set_empty_children() is generated for all props (with or without children). If the props struct doesn't have a children field this method is a no-op, but if it does, an empty Children value is used.

In case there is at least one child element, another method called set_children is used. This method is only generated for props which have a children field so we still get a compile error when trying to pass children to components that don't accept any.


EDIT: I should also mention that one can use #[prop_or_default] to allow optional children. My comment is focused on how to make it optional by default.

All 2 comments

This is a hard problem to solve.
The biggest problem here is that the html! macro has no knowledge of the Properties struct.
A consequence of this is that we have no idea whether the component has a children field or not.
In return, this means that we cannot simply pass an empty Children because that would break props without a children field.

Improving the error message is also, for the same reasons, a daunting task.

There's still hope though. We can turn children into a special case in the Properties derive macro and use setter methods to set it.
A method set_empty_children() is generated for all props (with or without children). If the props struct doesn't have a children field this method is a no-op, but if it does, an empty Children value is used.

In case there is at least one child element, another method called set_children is used. This method is only generated for props which have a children field so we still get a compile error when trying to pass children to components that don't accept any.


EDIT: I should also mention that one can use #[prop_or_default] to allow optional children. My comment is focused on how to make it optional by default.

I had a similar issue.

pub struct Props {
  pub children: Children,
  pub active: bool,
}

This threw an error at call-site when I didn't pass an active prop, which makes sense.

The strange thing was the actual error message "no method named children found for struct ..."

I had the impression I did something wrong with the children prop because the example on the Yew website uses a tuple struct, but in the end, it was because the active prop wasn't set to optional.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kellytk picture kellytk  路  4Comments

Boscop picture Boscop  路  4Comments

wldcordeiro picture wldcordeiro  路  4Comments

IngwiePhoenix picture IngwiePhoenix  路  4Comments

thienpow picture thienpow  路  3Comments