fermi.rs 1.1 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() -> Element {
  9. use_init_atom_root(cx);
  10. let name = use_read(&NAME);
  11. rsx! {
  12. div { "hello {name}!" }
  13. Child {}
  14. ChildWithRef {}
  15. }
  16. }
  17. fn Child() -> Element {
  18. let set_name = use_set(&NAME);
  19. 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() -> Element {
  28. let names = use_atom_ref(&NAMES);
  29. rsx! {
  30. div {
  31. ul {
  32. for name in names.read().iter() {
  33. li { "hello: {name}" }
  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. }