Expand description
§Event Handlers
Event Handlers let you react to user input in your application. In Dioxus, event handlers accept a closure that is called when the event occurs:
use dioxus::prelude::*;
fn App() -> Element {
rsx! {
button {
// The `onclick` event accepts a closure with the signature `fn(Event)`
onclick: |event_data| println!("clicked! I got the event data: {event_data:?}"),
"Click me"
}
}
}§Event Lifetimes
Events take a closure with the 'static lifetime. This means that the closure can only access data that either exists for the entire lifetime of the application, or data that you move into the closure.
State in dioxus is copy which makes it very easy to move into 'static closures like event handlers:
let mut count = use_signal(|| 0);
rsx! {
button {
// Since we added the `move` keyword, the closure will move the `count` signal into the closure
onclick: move |_| {
// This will panic because the `count` signal is not in scope
count.set(count() + 1);
},
"Click me"
}
};If you need to access data that is not Copy, you may need to clone the data before you move it into the closure:
// String is not `Copy`
let string = "hello world".to_string();
rsx! {
button {
// The string only has one owner. We could move it into this closure, but since we want to use the string in other closures later, we will clone it instead
onclick: {
// Clone the string in a new block
let string = string.clone();
// Then move the cloned string into the closure
move |_| println!("{}", string)
},
"Print hello world"
}
button {
// We don't use the string after this closure, so we can just move it into the closure directly
onclick: move |_| println!("{}", string),
"Print hello world again"
}
};§Async Event Handlers
In addition to closures that return nothing, you can also use async closures to handle events. If you return an async block from an event handler, dioxus will automatically spawn it:
use dioxus::prelude::*;
fn App() -> Element {
rsx! {
button {
// The `onclick` event can also accept a closure that returns an async block
onclick: move |_| async move {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
println!("You clicked the button one second ago!");
},
"Click me"
}
}
}Structs§
- The DragEvent interface is a DOM event that represents a drag and drop interaction. The user initiates a drag by placing a pointer device (such as a mouse) on the touch surface and then dragging the pointer to a new location (such as another DOM element). Applications are free to interpret a drag and drop interaction in an application-specific way.
- A form value that may either be a list of values or a single value
- An Element that has been rendered and allows reading and modifying information about it.
- A synthetic event that wraps a web-style
MouseEventData associated with a mouse event - A platform specific event.
- A serialized version of AnimationData
- A serialized version of ClipboardData
- A serialized version of CompositionData
- A serialized version of DragData
- A serialized version of FocusData
- A serialized form data object
- A serialized version of ImageData
- A serialized version of KeyboardData
- A serialized version of MediaData
- A serialized version of
MouseData - A serialized version of PointerData
- A serialized version of ResizeData
- A serialized version of ScrollData
- A serialized version of SelectionData
- A serialized version of ToggleData
- A serialized version of TouchData
- A serialized version of TransitionData
- A serialized version of VisibleData
- A serialized version of WheelData
- Data associated with a WheelEvent
Enums§
- The error type for the MountedData
- The error type for the MountedData
- The way that scrolling should be performed
- The error type for the VisibleData
Traits§
- A trait for any object that has the data for an animation event
- A trait for any object that has the data for a composition event
- A trait for any object that has the data for a drag event
- An object that has all the data for a form event
- A trait for any object that has the data for an image event
- A trait for any object that has the data for a mouse event
- A trait for any object that has the data for a pointer event
- A trait for touch point data
- A converter between a platform specific event and a general event. All code in a renderer that has a large binary size should be placed in this trait. Each of these functions should be snipped in high levels of optimization.
- An Element that has been rendered and allows reading and modifying information about it.
Functions§
- abort
- onanimationend
- onanimationiteration
- onanimationstart
- onblur
- canplay
- canplaythrough
- onchange
- Execute a callback when a button is clicked.
- oncompositionend
- oncompositionstart
- oncompositionupdate
- oncontextmenu
- oncopy
- oncut
- ondblclick
Deprecated General Event Handler Information
General Event Handler Information
- ondrag
- ondragend
- ondragenter
- ondragexit
- ondragleave
- ondragover
- ondragstart
- ondrop
- durationchange
- emptied
- encrypted
- ended
- onerror
- onfocus
General Event Handler Information
General Event Handler Information
- gotpointercapture
- The
oninputevent is fired when the value of a<input>,<select>, or<textarea>element is changed. - oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- loadeddata
- loadedmetadata
- loadstart
- lostpointercapture
- The onmounted event is fired when the element is first added to the DOM. This event gives you a
MountedDataobject and lets you interact with the raw DOM element. - onmousedown
- onmouseenter
- onmouseleave
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onpaste
- pause
- play
- playing
- pointercancel
- pointerdown
- pointerenter
- pointerleave
- pointermove
- pointerout
- pointerover
- pointerup
- progress
- ratechange
- onreset
- onresize
- onscroll
- seeked
- seeking
- select
- selectionchange
- selectstart
- stalled
- onsubmit
- suspend
- timeupdate
- ontoggle
- touchcancel
- touchend
- touchmove
- touchstart
- transitionend
- onvisible
- volumechange
- waiting
- Called when the mouse wheel is rotated over an element.
Type Aliases§
- The MountedResult type for the MountedData
- A synthetic event that wraps a web-style
PointerEvent - The ResizeResult type for the ResizeData
- The VisibleResult type for the VisibleData
- A synthetic event that wraps a web-style
WheelEvent