update the dioxus cratesio readme, add many more examples (#5501)
* update the dioxus cratesio readme * fix example * fix header * add a bunch more examples
This commit is contained in:
1 parent
74e7d20f80
commit
cd54f5953b
30 files changed
+1403
-235
No files matched your search
@@ -0,0 +1,61 @@
|
||||
//! A signal that holds a struct.
|
||||
//!
|
||||
//! Signals don't care what they hold — you can put any `'static` type inside. To mutate
|
||||
//! a field on a struct signal, call `.write()` and the guard derefs to the inner value.
|
||||
//! Any read of the signal in the UI will re-render when you drop the write guard.
|
||||
|
||||
use dioxus::prelude::*;
|
||||
|
||||
fn main() {
|
||||
dioxus::launch(app);
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct User {
|
||||
name: String,
|
||||
age: u32,
|
||||
admin: bool,
|
||||
}
|
||||
|
||||
fn app() -> Element {
|
||||
let mut user = use_signal(|| User {
|
||||
name: "Ada".to_string(),
|
||||
age: 36,
|
||||
admin: false,
|
||||
});
|
||||
|
||||
rsx! {
|
||||
h1 { "User profile" }
|
||||
p { "Name: {user.read().name}" }
|
||||
p { "Age: {user.read().age}" }
|
||||
p { "Admin: {user.read().admin}" }
|
||||
|
||||
button {
|
||||
// .write() gives a mutable guard — assign to a single field
|
||||
onclick: move |_| user.write().age += 1,
|
||||
"Happy birthday!"
|
||||
}
|
||||
button {
|
||||
onclick: move |_| {
|
||||
let mut guard = user.write();
|
||||
guard.admin = !guard.admin;
|
||||
},
|
||||
"Toggle admin"
|
||||
}
|
||||
|
||||
input {
|
||||
value: "{user.read().name}",
|
||||
oninput: move |evt| user.write().name = evt.value(),
|
||||
}
|
||||
|
||||
// Replacing the whole value works too
|
||||
button {
|
||||
onclick: move |_| user.set(User {
|
||||
name: "Grace".to_string(),
|
||||
age: 85,
|
||||
admin: true,
|
||||
}),
|
||||
"Reset to Grace"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user