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

props of type Option<T> cannot use None #3747

Open
arcollector opened this issue Oct 11, 2024 · 2 comments
Open

props of type Option<T> cannot use None #3747

arcollector opened this issue Oct 11, 2024 · 2 comments
Labels

Comments

@arcollector
Copy link

Problem

Hello there, I have this simple code

#[derive(Properties, PartialEq)]
pub struct Props {
    pub id_patient: Option<String>,
    pub id_report: Option<String>,
}

#[function_component(PatientsNavBar)]
pub fn patients_nav_bar(
    Props {
        id_patient,
        id_report,
    }: &Props,
) -> Html {
 html! { /* */ }
}

#[function_component(App)]
fn app() -> Html {
  html! {
     <PatientsNavBar id_patient={None} id_report={None} />
  }
}

the compiler outputs the following the error

type annotations needed
multiple `impl`s satisfying `std::option::Option<_>: IntoPropValue<std::option::Option<std::string::String>>` found in the `yew` crate:
- impl IntoPropValue<std::option::Option<std::string::String>> for std::option::Option<&'static str>;
- impl<T> IntoPropValue<T> for T

Could anybody bring me some light on this matter, how to set an optional prop ?

Environment:

  • Yew version: [ master]
  • Rust version: [1.81.1]
  • Target [wasm32-unknown-unknown]
  • Build tool [trunk]
@iemafzalhassan
Copy link

Hi 👋

can you please assign me this issue? I would like to work on it

@ryanobeirne
Copy link

@arcollector this is probably an issue with how the html!() macro parses things, but you can still use the fully annotated Option syntax: id_patient={Option::<String>::None}.

However, it may or may not be more appropriate to use one of the #[prop_or...] derive macros and simply omit the parameter:

#[derive(Properties, PartialEq)]
pub struct Props {
    #[prop_or_default] // default is empty string
    pub id_patient: String,
    #[prop_or_else(default_id_report)] // default calls function below
    pub id_report: String,
}

fn default_id_report() -> String {
    "000000".into()
}

#[function_component(PatientsNavBar)]
pub fn patients_nav_bar(
    Props {
        id_patient,
        id_report,
    }: &Props,
) -> Html {
 html! { /* */ }
}

#[function_component(App)]
fn app() -> Html {
  html! { <PatientsNavBar /> }
}

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

3 participants