What is Dioxus, How to Build?

Dec 11, 2024

Build for web, desktop, and mobile, and more with a single codebase. Zero-config setup, integrated hot-reloading, and signals-based state management. Add backend functionality with Server Functions and bundle with our CLI.

What is Dioxus, How to Build?

Dioxus: A Fullstack Cross-Platform App Framework for Rust

Dioxus is a fullstack and cross-platform framework for building web, desktop, mobile, and server apps using Rust. It offers a React-like development experience with features like components, props, and hooks, but with Rust's performance and safety. Key features include ergonomic state management, type-safe routing, an integrated bundler, and instant hot-reloading. Dioxus supports various platforms, including web (with SSR and hydration), desktop (macOS, Linux, Windows), and mobile (Android, iOS).

Building with Dioxus: A Step-by-Step Guide

The process of building with Dioxus involves several key steps:

1. Prerequisites:

2. Installation:

Install the Dioxus CLI (dx) using:

cargo install dioxus-cli

If you encounter OpenSSL errors, ensure the necessary dependencies are installed (refer to https://docs.rs/openssl/latest/openssl/#automatic for details).

3. Creating a New Project:

Use the CLI to create a new project:

dx new my-app
cd my-app

You'll be prompted to select a platform (Web, Desktop, Mobile, etc.), a styling library (e.g., vanilla CSS), and whether to enable routing.

4. Running Your App:

Start the development server:

dx serve

For LiveView templates, use dx serve --desktop. For web targets, the application will be served at a specified address.

5. Core Concepts:

  • Components: Reusable UI elements built using functions returning Element. The #[component] attribute simplifies component creation and prop management.
  • rsx! Macro: A JSX-like macro for creating elements with attributes, listeners, and children. Attributes and listeners are listed before children.
  • Hooks: Reusable logic units for managing state and side effects within components. They start with use_ (e.g., use_signal). Adhere to the "rules of hooks" for correct usage.

6. Example: A Simple Counter

A basic Dioxus counter app demonstrates core concepts:

use dioxus::prelude::*;

#[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" }
    }
}

7. Further Learning:

Explore the Dioxus documentation (https://dioxuslabs.com/learn/0.6/) for tutorials, guides, and advanced topics like server-side rendering, concurrent rendering, and platform-specific details. The Dioxus website also serves as a testbed for new features.

Dioxus Multiplatform Icon Dioxus Hot Reloading Dioxus Android and iOS Support Dioxus Bundling Dioxus Documentation Dioxus Community
Recent Posts