1
0

event_handler_prop.rs 733 B

123456789101112131415161718192021222324252627282930313233
  1. #![allow(non_snake_case)]
  2. use dioxus::events::MouseData;
  3. use dioxus::prelude::*;
  4. fn main() {
  5. dioxus_desktop::launch(App);
  6. }
  7. fn App(cx: Scope) -> Element {
  8. // ANCHOR: usage
  9. cx.render(rsx! {
  10. FancyButton {
  11. on_click: move |event| println!("Clicked! {event:?}")
  12. }
  13. })
  14. // ANCHOR_END: usage
  15. }
  16. // ANCHOR: component_with_handler
  17. #[derive(Props)]
  18. pub struct FancyButtonProps<'a> {
  19. on_click: EventHandler<'a, MouseData>,
  20. }
  21. pub fn FancyButton<'a>(cx: Scope<'a, FancyButtonProps<'a>>) -> Element<'a> {
  22. cx.render(rsx!(button {
  23. class: "fancy-button",
  24. onclick: move |evt| cx.props.on_click.call(evt),
  25. "click me pls."
  26. }))
  27. }
  28. // ANCHOR_END: component_with_handler