fermi.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #![allow(non_snake_case)]
  2. use dioxus::prelude::*;
  3. use fermi::*;
  4. fn main() {
  5. dioxus_desktop::launch(app)
  6. }
  7. static NAME: Atom<String> = Atom(|_| "world".to_string());
  8. fn app(cx: Scope) -> Element {
  9. use_init_atom_root(cx);
  10. let name = use_read(cx, &NAME);
  11. cx.render(rsx! {
  12. div { "hello {name}!" }
  13. Child {}
  14. ChildWithRef {}
  15. })
  16. }
  17. fn Child(cx: Scope) -> Element {
  18. let set_name = use_set(cx, &NAME);
  19. cx.render(rsx! {
  20. button {
  21. onclick: move |_| set_name("dioxus".to_string()),
  22. "reset name"
  23. }
  24. })
  25. }
  26. static NAMES: AtomRef<Vec<String>> = AtomRef(|_| vec!["world".to_string()]);
  27. fn ChildWithRef(cx: Scope) -> Element {
  28. let names = use_atom_ref(cx, &NAMES);
  29. cx.render(rsx! {
  30. div {
  31. ul {
  32. names.read().iter().map(|f| rsx!{
  33. li { "hello: {f}" }
  34. })
  35. }
  36. button {
  37. onclick: move |_| {
  38. let names = names.clone();
  39. cx.spawn(async move {
  40. names.write().push("asd".to_string());
  41. })
  42. },
  43. "Add name"
  44. }
  45. }
  46. })
  47. }