1
0

fermi.rs 1.2 KB

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