Components are the fundamental building blocks of Dioxus applications. They are reusable, composable functions that return UI elements.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/DioxusLabs/dioxus/llms.txt
Use this file to discover all available pages before exploring further.
Component Basics
A component is a function that returns anElement. The simplest component takes no arguments:
The Element Type
TheElement type is defined as:
The #[component] Macro
The#[component] macro is the recommended way to create components with props. It automatically generates a props struct and handles component registration.
Basic Usage
Component Naming
Components must use:- PascalCase (recommended):
MyComponent,UserProfile - snake_case with underscore:
my_component,user_profile
#[component(no_case_check)] if needed.
Props and Parameters
Components can accept parameters which become props:Using Components
Call components inrsx! like HTML elements:
Children
Components can accept children by including achildren: Element parameter:
Component Function Trait
Components must implement theComponentFunction trait. This is automatically done for:
- Functions with signature
fn() -> Element - Functions with signature
fn(Props) -> Elementwhere Props implementsProperties - Functions marked with
#[component]
Component Lifecycle
Components in Dioxus:- First render: Component function is called with props
- Re-render: When props change or state updates, component re-runs
- Cleanup: When component is unmounted, hooks clean up automatically
Best Practices
Keep Components Small
Break down complex UIs into smaller, focused components for better reusability and testing.
Use PascalCase
Follow Rust conventions and use PascalCase for component names to distinguish them from HTML elements.
Props Should Be PartialEq
Props must implement
PartialEq for memoization - this prevents unnecessary re-renders.Return Element
Always return
Element from components. Use rsx! macro to construct UI.