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:
v0.171.47.0-beta.3wasm32-unknown-unknownweb-sysQuestionnaire
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.
Most helpful comment
This is a hard problem to solve.
The biggest problem here is that the
html!macro has no knowledge of thePropertiesstruct.A consequence of this is that we have no idea whether the component has a
childrenfield or not.In return, this means that we cannot simply pass an empty
Childrenbecause that would break props without achildrenfield.Improving the error message is also, for the same reasons, a daunting task.
There's still hope though. We can turn
childreninto a special case in thePropertiesderive macro and use setter methods to set it.A method
set_empty_children()is generated for all props (with or withoutchildren). If the props struct doesn't have achildrenfield this method is a no-op, but if it does, an emptyChildrenvalue is used.In case there is at least one child element, another method called
set_childrenis used. This method is only generated for props which have achildrenfield 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.