Description
Problem:
While working on yew-alert
, I noticed that std::default::Default
is not implemented for yew::UseStateHandle
. This could be beneficial when implementing the Default
trait for a component's Props
. For instance, in the case of AlertProps
, it would resemble:
impl Default for AlertProps {
fn default() -> Self {
AlertProps {
message: Default::default(),
timeout: Default::default(),
show_alert: Default::default(), // Error: the trait `std::default::Default` is not implemented for `yew::UseStateHandle<bool>`
// show_alert: use_state(|| true), // <--- This thing should work, but it is returning opaque type something
// ...snip...
}
}
}
This functionality is useful for allowing users to initialize the props struct by only passing some of the props values, with the others set to default, like so:
let props = AlertProps {
message: "Hello",
..AlertProps::default()
}
And then consume it in a component like so:
html!{
<Alert ..props />
}
The same should apply to the prop_or
attribute:
pub struct AlertProps {
// ...snip...
/// State handle to control the visibility of the alert.
#[prop_or(use_state(|| true)]
pub show_alert: UseStateHandle<bool>,
// ...snip...
html!{
<Alert message="Hello" />
}
Steps To Reproduce:
Attempt to implement Default
for props containing a UseStateHandle
field type.
Expected Behavior:
The UseStateHandle
struct should have an implementation of the Default
trait.
Environment:
- Yew version: 0.21.0
- Rust version: 1.75.0
- Target:
wasm32-unknown-unknown
- Build Tool: trunk
- OS: Any
- Browser and Version: Any
Questionnaire:
- I'm interested in fixing this myself but don't know where to start
- I would like to fix it and I have a solution
- I don't have time to fix this right now, but maybe later
Note
This feature is useful to adhere to the DRY principle and decouple logic from UI as shown in this example.