#attribute

5 messages · Page 1 of 1 (latest)

thorny gyro
#

Is there a way to use if inside an element to render an attribute or not render it?
I'm not used to React and rsx DLS

I want to prevent

// cx.props.style is Option<&'a str>
rsx!(
  div {
    style: cx.props.style.unwrap_or_default()
  }
)

to render <div style> if cx.props.style is None

spring eagle
#

You can pass in Option<&str> to style:

#![allow(non_snake_case)]

use dioxus::prelude::*;

fn main() {
    dioxus_web::launch(app);
}

fn app(cx: Scope) -> Element {
    let style: Option<&str> = None;
    cx.render(rsx! {
        div {
            style: style,
        }
    })
}

will render <div></div>

and

#![allow(non_snake_case)]

use dioxus::prelude::*;

fn main() {
    dioxus_web::launch(app);
}

fn app(cx: Scope) -> Element {
    let style: Option<&str> = Some("background-color: red;");
    cx.render(rsx! {
        div {
            style: style,
        }
    })
}

will render <div style="background-color: red;"></div>

thorny gyro
#

I tried that, but it complained that Option<&str> does not implement Display

spring eagle