2022-02-17 15:38:51 +00:00
|
|
|
#![allow(non_snake_case)]
|
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
2022-07-09 19:15:20 +00:00
|
|
|
use fermi::*;
|
2022-02-17 15:38:51 +00:00
|
|
|
|
|
|
|
fn main() {
|
2022-07-09 19:15:20 +00:00
|
|
|
dioxus_desktop::launch(app)
|
2022-02-17 15:38:51 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 12:09:11 +00:00
|
|
|
static NAME: Atom<String> = Atom(|_| "world".to_string());
|
2022-02-17 15:38:51 +00:00
|
|
|
|
|
|
|
fn app(cx: Scope) -> Element {
|
2023-01-12 00:13:41 +00:00
|
|
|
use_init_atom_root(cx);
|
2023-06-21 12:09:11 +00:00
|
|
|
let name = use_read(cx, &NAME);
|
2022-02-17 15:38:51 +00:00
|
|
|
|
|
|
|
cx.render(rsx! {
|
|
|
|
div { "hello {name}!" }
|
|
|
|
Child {}
|
2022-11-16 07:22:41 +00:00
|
|
|
ChildWithRef {}
|
2022-02-17 15:38:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn Child(cx: Scope) -> Element {
|
2023-06-21 12:09:11 +00:00
|
|
|
let set_name = use_set(cx, &NAME);
|
2022-02-17 15:38:51 +00:00
|
|
|
|
|
|
|
cx.render(rsx! {
|
|
|
|
button {
|
|
|
|
onclick: move |_| set_name("dioxus".to_string()),
|
|
|
|
"reset name"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-04-25 00:47:22 +00:00
|
|
|
|
2023-06-21 12:09:11 +00:00
|
|
|
static NAMES: AtomRef<Vec<String>> = AtomRef(|_| vec!["world".to_string()]);
|
2022-04-25 00:47:22 +00:00
|
|
|
|
|
|
|
fn ChildWithRef(cx: Scope) -> Element {
|
2023-06-21 12:09:11 +00:00
|
|
|
let names = use_atom_ref(cx, &NAMES);
|
2022-04-25 00:47:22 +00:00
|
|
|
|
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
ul {
|
2024-01-11 03:33:34 +00:00
|
|
|
for name in names.read().iter() {
|
|
|
|
li { "hello: {name}" }
|
|
|
|
}
|
2022-04-25 00:47:22 +00:00
|
|
|
}
|
|
|
|
button {
|
|
|
|
onclick: move |_| {
|
|
|
|
let names = names.clone();
|
|
|
|
cx.spawn(async move {
|
|
|
|
names.write().push("asd".to_string());
|
|
|
|
})
|
2022-04-25 00:51:52 +00:00
|
|
|
},
|
|
|
|
"Add name"
|
2022-04-25 00:47:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|