basic.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //! Basic example that renders a simple VNode to the browser.
  2. use dioxus::events::on::MouseEvent;
  3. use dioxus_core as dioxus;
  4. use dioxus_core::prelude::*;
  5. use dioxus_html_namespace as dioxus_elements;
  6. use dioxus_web::*;
  7. fn main() {
  8. // Setup logging
  9. wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
  10. console_error_panic_hook::set_once();
  11. // Run the app
  12. wasm_bindgen_futures::spawn_local(WebsysRenderer::start(App));
  13. }
  14. static App: FC<()> = |cx| {
  15. let (state, set_state) = use_state_classic(cx, || 0);
  16. cx.render(rsx! {
  17. div {
  18. section { class: "py-12 px-4 text-center"
  19. div { class: "w-full max-w-2xl mx-auto"
  20. span { class: "text-sm font-semibold"
  21. "count: {state}"
  22. }
  23. div {
  24. C1 {
  25. onclick: move |_| set_state(state + 1)
  26. "incr"
  27. }
  28. C1 {
  29. onclick: move |_| set_state(state - 1)
  30. "decr"
  31. }
  32. }
  33. }
  34. }
  35. }
  36. })
  37. };
  38. #[derive(Props)]
  39. struct IncrementerProps<'a> {
  40. onclick: &'a dyn Fn(MouseEvent),
  41. }
  42. fn C1<'a, 'b>(cx: Context<'a, IncrementerProps<'b>>) -> VNode<'a> {
  43. cx.render(rsx! {
  44. button {
  45. class: "inline-block py-4 px-8 mr-6 leading-none text-white bg-indigo-600 hover:bg-indigo-900 font-semibold rounded shadow"
  46. onclick: {cx.onclick}
  47. "becr"
  48. {cx.children()}
  49. }
  50. })
  51. }