Overview
Components are the building blocks of Dioxus applications. They are functions that take props and return anElement. The #[component] macro simplifies component creation with automatic props derivation.
Component Type
Element.
ComponentFunction Trait
TheComponentFunction trait is implemented for functions that can act as components.
Implementations
The trait is automatically implemented for:- Functions that take props:
fn(Props) -> Element - Functions with no props:
fn() -> Element
The #[component] Macro
The#[component] macro is the recommended way to define components. It automatically generates the props struct and implements required traits.
Basic Usage
With Multiple Props
Optional Props
Children Prop
Manual Props Definition
You can also manually define props using theProps trait.
Properties Trait
Manual Implementation
VComponent
The internal representation of a component instance.Methods
VComponent::new()
Creates a new component instance.
VComponent::mounted_scope_id()
Gets the ScopeId this component is mounted to.
Component Patterns
Stateful Components
Components with Effects
Higher-Order Components
Generic Components
Component Lifecycle
Initialization
Cleanup
Prop Validation
Props must implementPartialEq for memoization:
PartialEq).
Best Practices
Keep Components Small
Use Memo for Expensive Props
See Also
- Element - Return type of components
- Scope - Component context and state
- Properties Documentation