dioxus/examples/fermi.rs

56 lines
1.2 KiB
Rust
Raw Normal View History

2022-02-17 15:38:51 +00:00
#![allow(non_snake_case)]
use dioxus::prelude::*;
fn main() {
dioxus::desktop::launch(app)
}
static NAME: Atom<String> = |_| "world".to_string();
fn app(cx: Scope) -> Element {
let name = use_read(&cx, NAME);
cx.render(rsx! {
div { "hello {name}!" }
Child {}
2022-04-25 00:47:22 +00:00
ChildWithRef{}
2022-02-17 15:38:51 +00:00
})
}
fn Child(cx: Scope) -> Element {
let set_name = use_set(&cx, NAME);
cx.render(rsx! {
button {
onclick: move |_| set_name("dioxus".to_string()),
"reset name"
}
})
}
2022-04-25 00:47:22 +00:00
static NAMES: AtomRef<Vec<String>> = |_| vec!["world".to_string()];
fn ChildWithRef(cx: Scope) -> Element {
let names = use_atom_ref(&cx, NAMES);
cx.render(rsx! {
div {
ul {
names.read().iter().map(|f| rsx!{
li { "hello: {f}" }
})
}
button {
onclick: move |_| {
let names = names.clone();
cx.spawn(async move {
names.write().push("asd".to_string());
})
},
"Add name"
2022-04-25 00:47:22 +00:00
}
}
})
}