Skip to content
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

[Feature]: Provide a Default trait implementation for the UseStateHandle struct #3597

Open
1 of 3 tasks
wiseaidev opened this issue Feb 3, 2024 · 2 comments
Open
1 of 3 tasks
Labels

Comments

@wiseaidev
Copy link

wiseaidev commented Feb 3, 2024

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.

@wiseaidev wiseaidev added the bug label Feb 3, 2024
@dospore
Copy link

dospore commented Feb 4, 2024

This seems like a nice first issue, im open to picking this up if you guys agree. Will probably require some generics and working with traits but am open to the challenge

@dospore
Copy link

dospore commented Feb 6, 2024

@wiseaidev after having a look it seems pretty complex. We could make it work by giving an empty closure as the dispatch function but would not achieve much since you can just do that on your side instead and pass a default show_alert closure instead of a UseStateHandler. The opaque type comes from the hook macro which allows the reducers to access the HookContext which gets created when the function component is created. Since this is props im not sure that can be instantiated before then and is why hooks must be called inside the fc.

The usecase might not be important enough for its complexity (if it is complex). It feels like there might be a way to do it but will have to wait for more guidance from someone a bit more experience with the codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants