yewstack / yew

Rust / Wasm framework for creating reliable and efficient web applications
https://yew.rs
Apache License 2.0
30.5k stars 1.42k forks source link

conditional 'if' doesn't work with ChildrenRenderer<T> #3157

Closed ctron closed 1 year ago

ctron commented 1 year ago

Problem

When using ChildrenRenderer<T> (instead of Children), then the conditional if doesn't seem to work.

Steps To Reproduce

#[function_component(Bar)]
fn bar() -> Html { html!() }

#[derive(PartialEq, Properties)]
struct FooProperties {
    pub childern: ChildrenRenderer<Bar>,
}

#[function_component(Foo)]
fn foo(props: &FooProperties) -> Html {}

#[function_component(Example)]
fn example() -> Html {
  let some_switch = false;
  html!(
    <Foo>
        <Bar/>
        if some_switch {
            <Bar/>
        }
        <Bar/>
    </Foo>
  )
}

Expected behavior

I would expect this to work the same way as with Children.

Environment:

Questionnaire

ctron commented 1 year ago

It looks like a possible workaround is to use:

#[function_component(Example)]
fn example() -> Html {
  let some_switch = false;
  html!(
    <Foo>
        <Bar/>
        { if some_switch {
            Some(html_nested!(<Bar/>))
        } else { None } }
        <Bar/>
    </Foo>
  )
}
ranile commented 1 year ago

The issue is discussed more in depth in #3256. Please continue the discussion there