selectors.rs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. use dioxus_core::prelude::*;
  2. use recoil::*;
  3. const A: Atom<i32> = |_| 0;
  4. const B: Atom<i32> = |_| 0;
  5. const C: Selector<i32> = |api| api.get(&A) + api.get(&B);
  6. static App: FC<()> = |ctx| {
  7. use_init_recoil_root(ctx, |_| {});
  8. rsx! { in ctx,
  9. div {
  10. Banner {}
  11. BtnA {}
  12. BtnB {}
  13. }
  14. }
  15. };
  16. static Banner: FC<()> = |ctx| {
  17. let count = use_read(&ctx, &C);
  18. ctx.render(rsx! { h1 { "Count: {count}" } })
  19. };
  20. static BtnA: FC<()> = |ctx| {
  21. let (a, set) = use_read_write(&ctx, &A);
  22. rsx! { in ctx,
  23. div { "a"
  24. button { "+", onclick: move |_| set(a + 1) }
  25. button { "-", onclick: move |_| set(a - 1) }
  26. }
  27. }
  28. };
  29. static BtnB: FC<()> = |ctx| {
  30. let (b, set) = use_read_write(&ctx, &B);
  31. rsx! { in ctx,
  32. div { "b"
  33. button { "+", onclick: move |_| set(b + 1) }
  34. button { "-", onclick: move |_| set(b - 1) }
  35. }
  36. }
  37. };
  38. fn main() {
  39. wasm_bindgen_futures::spawn_local(dioxus_web::WebsysRenderer::start(App))
  40. }