Dioxus 是一个 Rust 的全栈跨平台应用框架,支持服务端渲染、实时更新和类似 React 的组件模型。当用户需要使用 Dioxus 构建 Web 应用、创建组件、处理状态、路由、或处理数据获取时使用。
Dioxus 是一个现代 Rust Web 框架,结合了 React 的编程模型与 Rust 的性能优势。它允许你构建高性能的 Web 应用,支持服务端渲染(SSR)、实时更新和跨平台部署。
bash
src/
├── main.rs # 应用入口
├── App.rsx # 根组件
├── components/ # 子组件目录
├── routes/ # 路由模块
├── server_fn/ # 服务端函数
└── utils/ # 工具函数
组件是使用 #[component] 宏定义的函数,它接受 Properties(props)并返回一个 Element。
rust
use dioxus::prelude::*;
#[component]
fn DogApp(breed: String) -> Element {
rsx! {
div { class: card } {
h2 { Dogs of breed: {breed} }
ul {
(0..3).map(|i| rsx! {
li { Dog {i} }
})
}
}
}
}
所有组件都接受一个描述其参数的 struct。Props 需要 derive 以下 traits:
rust
#[derive(Props, PartialEq, Clone)]
struct DogAppProps {
breed: String,
}
重要特性:
| 特性 | 说明 |
|---|---|
| Props | 组件接受的参数对象 |
| PartialEqual |
Dioxus 组件会在以下情况下重新渲染:
重要: Dioxus 默认 memoized 所有组件,性能比 React 更好。
Dioxus 提供类似 React 的 hooks 用于管理状态。
创建响应式状态:
rust
#[component]
fn Counter() -> Element {
let mut count = use_signal(cx, || 0);
rsx! {
div {
button {
onclick: move |_| {
count += 1;
}
} { Increment }
p { Count: {count} }
button {
onclick: move |_| {
count -= 1;
}
} { Decrement }
}
}
}
运行异步操作:
rust
#[component]
fn DataFetcher() -> Element {
let mut data = use_signal(cx, || None::
let loading = use_signal(cx, || false);
use_coroutine(cx, |cx| async move {
loading.set(true);
// 模拟异步数据获取
tokio::time::sleep(Duration::from_secs(2)).await;
data.set(Some(Fetched data.to_string()));
loading.set(false);
});
rsx! {
div {
if *loading.get() {
p { Loading... }
} else if let Some(d) = data.get() {
p { Data: {d} }
} else {
p { No data yet }
}
}
}
}
访问上下文值:
rust
#[derive(Clone, Props)]
struct ChildProps {
value: String,
}
#[component]
fn Child(props: ChildProps) -> Element {
rsx! { p { Value from context: {props.value} } }
}
#[component]
fn Parent() -> Element {
let value = usecontext(cx, || default.tostring());
rsx! { Child { value: value.clone() } }
}
使用信号进行全局状态管理:
rust
// main.rs
use dioxus::prelude::*;
fn main() {
let themesignal = RwSignal::new(light.tostring());
dioxus::launch(App, (
AppContext::new(theme_signal)
));
}
// App.rsx
#[component]
fn App() -> Element {
let theme = use_context(cx);
rsx! {
div {
button {
onclick: move || theme.set(dark.tostring())
} { Dark Mode }
button {
onclick: move || theme.set(light.tostring())
} { Light Mode }
p { Current theme: {theme.read()} }
}
}
}
rust
// routes/mod.rs
use dioxus::prelude::*;
use dioxus::router::{Route, Router};
#[component]
fn Home() -> Element {
rsx! { h1 { Home Page } }
}
#[component]
fn About() -> Element {
rsx! { h1 { About Page } }
}
// App.rsx
#[component]
fn App() -> Element {
rsx! {
Router {
Route { to: /, Home {} }
Route { to: /about, About {} }
}
}
}
rust
use dioxus::router::NavLink;
rsx! {
nav {
ul {
li { NavLink { to: / } { Home } }
li { NavLink { to: /about } { About } }
}
}
}
rust
#[derive(Props, Clone)]
struct FormProps {
on_submit: Callback
}
#[component]
fn Form(props: FormProps) -> Element {
let mut name = use_signal(cx, || String::new());
let mut email = use_signal(cx, || String::new());
rsx! {
form {
onsubmit: move |event| {
event.prevent_default();
props.on_submit.call((
name.get().clone(),
email.get().clone(),
));
}
} {
input {
r#type: text,
value: {name},
oninput: move |e| name.set(e.value())
}
input {
r#type: email,
value: {email},
oninput: move |e| email.set(e.value())
}
button { r#type: submit } { Submit }
}
}
}
rust
// main.rs
use dioxus::prelude::*;
fn main() {
// 启用 SSR 功能
dioxus::launch(App);
}
// server_fn/api.rs
use dioxus::prelude::*;
#[server_fn(/)]
async fn serve_home() -> String {
// 渲染组件为字符串
rendertostring(|cx| rsx! { App {} })
}
rust
use dioxus::prelude::*;
#[server_fn(/api/hello)]
async fn hello(name: String) -> String {
format!(Hello, {}!, name)
}
rust
#[server_fn(/api/data)]
async fn fetch_data() -> Json
use crate::data::fetchfromdb().await
}
rust
rsx! {
div {
if *count.get() > 0 {
p { Count is positive }
} else {
p { Count is zero }
}
}
}
rust
let items = vec![Item 1, Item 2, Item 3];
rsx! {
ul {
items.iter().map(|item| rsx! {
li { key: {item} } { item }
})
}
}
rust
#[component]
fn Header() -> Element {
rsx! {
header {
h1 { My App }
nav {
a { href: / } { Home }
a { href: /about } { About }
}
}
}
}
#[component]
fn PageLayout(content: Element) -> Element {
rsx! {
div { class: layout } {
Header {}
main { content }
Footer {}
}
}
}
rust
// 在 main.rs 或
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 dioxus-framework-1776318025 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 dioxus-framework-1776318025 技能
skillhub install dioxus-framework-1776318025
文件大小: 10.79 KB | 发布时间: 2026-4-16 18:44