You are currently viewing the docs for Dioxus 0.3.2 which is no longer maintained.

Describing the UI

Dioxus is a declarative framework. This means that instead of telling Dioxus what to do (e.g. to "create an element" or "set the color to red") we simply declare what we want the UI to look like using RSX.

You have already seen a simple example of RSX syntax in the "hello world" application:

src/hello_world_desktop.rs
// define a component that renders a div with the text "Hello, world!"
fn App(cx: Scope) -> Element {
    cx.render(rsx! {
        div {
            "Hello, world!"
        }
    })
}

Here, we use the rsx! macro to declare that we want a div element, containing the text "Hello, world!". Dioxus takes the RSX and constructs a UI from it.

RSX Features

RSX is very similar to HTML in that it describes elements with attributes and children. Here's an empty div element in RSX, as well as the resulting HTML:

src/rsx_overview.rs
cx.render(rsx!(div {
// attributes / listeners
// children
}))
<div></div>

Attributes

Attributes (and listeners) modify the behavior or appearance of the element they are attached to. They are specified inside the {} brackets, using the name: value syntax. You can provide the value as a literal in the RSX:

src/rsx_overview.rs
cx.render(rsx!(a {
href: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
class: "primary_button",
color: "red",
}))
<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" class="primary_button" autofocus="true" style="color: red"></a>

Note: All attributes defined in dioxus-html follow the snake_case naming convention. They transform their snake_case names to HTML's camelCase attributes.

Note: Styles can be used directly outside of the style: attribute. In the above example, color: "red" is turned into style="color: red".

Custom Attributes

Dioxus has a pre-configured set of attributes that you can use. RSX is validated at compile time to make sure you didn't specify an invalid attribute. If you want to override this behavior with a custom attribute name, specify the attribute in quotes:

src/rsx_overview.rs
cx.render(rsx!(b {
    "customAttribute": "value",
}))
<b customAttribute="value">
</b>

Interpolation

Similarly to how you can format Rust strings, you can also interpolate in RSX text. Use {variable} to Display the value of a variable in a string, or {variable:?} to use the Debug representation:

src/rsx_overview.rs
let coordinates = (42, 0);
let country = "es";
cx.render(rsx!(div {
class: "country-{country}",
"position": "{coordinates:?}",
// arbitrary expressions are allowed,
// as long as they don't contain `{}`
div {
    "{country.to_uppercase()}"
},
div {
    "{7*6}"
},
// {} can be escaped with {{}}
div {
    "{{}}"
},
}))
<div class="country-es" position="(42, 0)">
    <div>ES</div>
    <div>42</div>
    <div>{}</div>
</div>

Children

To add children to an element, put them inside the {} brackets after all attributes and listeners in the element. They can be other elements, text, or components. For example, you could have an ol (ordered list) element, containing 3 li (list item) elements, each of which contains some text:

src/rsx_overview.rs
cx.render(rsx!(ol {
li {"First Item"}
li {"Second Item"}
li {"Third Item"}
}))
<ol>
    <li>First Item</li>
    <li>Second Item</li>
    <li>Third Item</li>
</ol>

Fragments

You can render multiple elements at the top level of rsx! and they will be automatically grouped.

src/rsx_overview.rs
cx.render(rsx!(
p {"First Item"},
p {"Second Item"},
))
<p>First Item</p>
<p>Second Item</p>

Expressions

You can include arbitrary Rust expressions as children within RSX that implements IntoDynNode. This is useful for displaying data from an iterator:

src/rsx_overview.rs
let text = "Dioxus";
cx.render(rsx!(span {
text.to_uppercase(),
// create a list of text from 0 to 9
(0..10).map(|i| rsx!{ i.to_string() })
}))
<span>DIOXUS0123456789</span>

Loops

In addition to iterators you can also use for loops directly within RSX:

src/rsx_overview.rs
cx.render(rsx!{
// use a for loop where the body itself is RSX
div {
    // create a list of text from 0 to 9
    for i in 0..3 {
        // NOTE: the body of the loop is RSX not a rust statement
        div {
            "{i}"
        }
    }
}
// iterator equivalent
div {
    (0..3).map(|i| rsx!{ div { "{i}" } })
}
})
<div>0</div>
<div>1</div>
<div>2</div>
<div>0</div>
<div>1</div>
<div>2</div>

If statements

You can also use if statements without an else branch within RSX:

src/rsx_overview.rs
cx.render(rsx!{
// use if statements without an else
if true {
    rsx!(div { "true" })
}
})
<div>true</div>