fermi.rs 1.2 KB

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