#user-interface #ui-framework #cross-platform-ui #ui-component #ui #dom

dioxus

Portable, performant, and ergonomic framework for building cross-platform user interfaces in Rust

29 releases

0.6.0-alpha.5 Nov 16, 2024
0.6.0-alpha.3 Oct 14, 2024
0.5.6 Jul 18, 2024
0.5.0 Mar 28, 2024
0.1.0 Jan 20, 2021

#24 in GUI

Download history 3927/week @ 2024-08-08 3912/week @ 2024-08-15 3746/week @ 2024-08-22 4481/week @ 2024-08-29 3969/week @ 2024-09-05 4013/week @ 2024-09-12 3716/week @ 2024-09-19 4283/week @ 2024-09-26 3073/week @ 2024-10-03 3339/week @ 2024-10-10 4084/week @ 2024-10-17 4033/week @ 2024-10-24 3396/week @ 2024-10-31 2697/week @ 2024-11-07 2824/week @ 2024-11-14 2815/week @ 2024-11-21

12,290 downloads per month
Used in 112 crates (106 directly)

MIT/Apache

740KB
12K SLoC

🌗🚀 Dioxus

A concurrent, functional, virtual DOM for Rust

Resources

This overview provides a brief introduction to Dioxus. For a more in-depth guide, make sure to check out:

Overview and Goals

Dioxus makes it easy to quickly build complex user interfaces with Rust. Any Dioxus app can run in the web browser, as a desktop app, as a mobile app, or anywhere else provided you build the right renderer.

Dioxus is heavily inspired by React, supporting many of the same concepts:

  • Hooks for state
  • VirtualDom & diffing
  • Concurrency, fibers, and asynchronous rendering
  • JSX-like templating syntax

If you know React, then you know Dioxus.

Dioxus is substantially more performant than many of the other Rust UI libraries (Yew/Percy) and is significantly more performant than React—roughly competitive with InfernoJS.

Remember: Dioxus is a library for declaring interactive user interfaces—it is not a dedicated renderer. Most 1st party renderers for Dioxus currently only support web technologies.

Brief Overview

All Dioxus apps are built by composing functions that start with a capital letter and return an Element.

To launch an app, we use the launch method and use features in Cargo.toml to specify which renderer we want to use. In the launch function, we pass the app's root Component.

use dioxus::prelude::*;

fn main() {
    dioxus::launch(App);
}

// The #[component] attribute streamlines component creation.
// It's not required, but highly recommended. It will lint incorrect component definitions and help you create props structs.
#[component]
fn App() -> Element {
    rsx! { "hello world!" }
}

Elements & your first component

You can use the rsx! macro to create elements with a jsx-like syntax. Any element in rsx! can have attributes, listeners, and children. For consistency, we force all attributes and listeners to be listed before children.

# use dioxus::prelude::*;
let value = "123";

rsx! {
    div {
        class: "my-class {value}",                  // <--- attribute
        onclick: move |_| println!("clicked!"),   // <--- listener
        h1 { "hello world" }                       // <--- child
    }
};

The rsx! macro accepts attributes in "struct form". Any rust expression contained within curly braces that implements IntoDynNode will be parsed as a child. We make two exceptions: both for loops and if statements are parsed where their body is parsed as a rsx nodes.

# use dioxus::prelude::*;
rsx! {
    div {
        for _ in 0..10 {
            span { "hello world" }
        }
    }
};

Putting everything together, we can write a simple component that renders a list of elements:

# use dioxus::prelude::*;
#[component]
fn App() -> Element {
    let name = "dave";
    rsx! {
        h1 { "Hello, {name}!" }
        div { class: "my-class", id: "my-id",
            for i in 0..5 {
                div { "FizzBuzz: {i}" }
            }
        }
    }
}

Components

We can compose these function components to build a complex app. Each new component we design must take some Properties. For components with no explicit properties, we can omit the type altogether.

In Dioxus, all properties are memorized by default with Clone and PartialEq. For props you can't clone, simply wrap the fields in a ReadOnlySignal and Dioxus will handle converting types for you.

# use dioxus::prelude::*;
# #[component] fn Header(title: String, color: String) -> Element { todo!() }
#[component]
fn App() -> Element {
    rsx! {
        Header {
            title: "My App",
            color: "red",
        }
    }
}

The #[component] macro will help us automatically create a props struct for our component:

# use dioxus::prelude::*;
// The component macro turns the arguments for our function into named fields we can pass in to the component in rsx
#[component]
fn Header(title: String, color: String) -> Element {
    rsx! {
        div {
            background_color: "{color}",
            h1 { "{title}" }
        }
    }
}

You can read more about props in the reference.

Hooks

While components are reusable forms of UI elements, hooks are reusable forms of logic. Hooks provide a way of retrieving state from Dioxus' internal Scope and using it to render UI elements.

By convention, all hooks are functions that should start with use_. We can use hooks to define the state and modify it from within listeners.

# use dioxus::prelude::*;
#[component]
fn App() -> Element {
    // The use signal hook runs once when the component is created and then returns the current value every run after the first
    let name = use_signal(|| "world");

    rsx! { "hello {name}!" }
}

Hooks are sensitive to how they are used. To use hooks, you must abide by the "rules of hooks":

  • Hooks can only be called in the body of a component or another hook. Not inside of another expression like a loop, conditional or function call.
  • Hooks should start with "use_"

In a sense, hooks let us add a field of state to our component without declaring an explicit state struct. However, this means we need to "load" the struct in the right order. If that order is wrong, then the hook will pick the wrong state and panic.

Dioxus includes many built-in hooks that you can use in your components. If those hooks don't fit your use case, you can also extend Dioxus with custom hooks.

Putting it all together

Using components, rsx, and hooks, we can build a simple app.

use dioxus::prelude::*;

fn main() {
    dioxus::launch(App);
}

#[component]
fn App() -> Element {
    let mut count = use_signal(|| 0);

    rsx! {
        div { "Count: {count}" }
        button { onclick: move |_| count += 1, "Increment" }
        button { onclick: move |_| count -= 1, "Decrement" }
    }
}

Conclusion

This overview doesn't cover everything. Make sure to check out the tutorial and reference on the official website for more details.

Beyond this overview, Dioxus supports:

  • Server-side rendering
  • Concurrent rendering (with async support)
  • Web/Desktop/Mobile support
  • Pre-rendering and hydration
  • Fragments, and Suspense
  • Inline-styles
  • Custom event handlers
  • Custom elements
  • Basic fine-grained reactivity (IE SolidJS/Svelte)
  • and more!

Build cool things ✌️

Dependencies

~2–47MB
~710K SLoC