basic.rs 1.6 KB

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