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§
- Animation
Data - Clipboard
Data - Composition
Data - DOMRect
- Drag
Data - 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.
- Focus
Data - Form
Data - Form
Value - A form value that may either be a list of values or a single value
- Image
Data - Keyboard
Data - Media
Data - Mounted
Data - An Element that has been rendered and allows reading and modifying information about it.
- Mouse
Data - A synthetic event that wraps a web-style
MouseEvent
Data associated with a mouse event - Platform
Event Data - A platform specific event.
- Pointer
Data - Resize
Data - Scroll
Data - Selection
Data - Serialized
Animation Data - A serialized version of AnimationData
- Serialized
Clipboard Data - A serialized version of ClipboardData
- Serialized
Composition Data - A serialized version of CompositionData
- Serialized
Drag Data - A serialized version of DragData
- Serialized
Focus Data - A serialized version of FocusData
- Serialized
Form Data - A serialized form data object
- Serialized
Image Data - A serialized version of ImageData
- Serialized
Keyboard Data - A serialized version of KeyboardData
- Serialized
Media Data - A serialized version of MediaData
- Serialized
Mouse Data - A serialized version of
MouseData
- Serialized
Pointer Data - A serialized version of PointerData
- Serialized
Resize Data - A serialized version of ResizeData
- Serialized
Scroll Data - A serialized version of ScrollData
- Serialized
Selection Data - A serialized version of SelectionData
- Serialized
Toggle Data - A serialized version of ToggleData
- Serialized
Touch Data - A serialized version of TouchData
- Serialized
Transition Data - A serialized version of TransitionData
- Serialized
Visible Data - A serialized version of VisibleData
- Serialized
Wheel Data - A serialized version of WheelData
- Toggle
Data - Touch
Data - Touch
Point - Transition
Data - Visible
Data - Wheel
Data - Data associated with a WheelEvent
Enums§
- KeyCode
- Mounted
Error - The error type for the MountedData
- Resize
Error - The error type for the MountedData
- Scroll
Behavior - The way that scrolling should be performed
- Visible
Error - The error type for the VisibleData
Traits§
- HasAnimation
Data - A trait for any object that has the data for an animation event
- HasClipboard
Data - HasComposition
Data - A trait for any object that has the data for a composition event
- HasDrag
Data - A trait for any object that has the data for a drag event
- HasFocus
Data - HasForm
Data - An object that has all the data for a form event
- HasImage
Data - A trait for any object that has the data for an image event
- HasKeyboard
Data - HasMedia
Data - HasMouse
Data - A trait for any object that has the data for a mouse event
- HasPointer
Data - A trait for any object that has the data for a pointer event
- HasResize
Data - HasScroll
Data - HasSelection
Data - HasToggle
Data - HasTouch
Data - HasTouch
Point Data - A trait for touch point data
- HasTransition
Data - HasVisible
Data - HasWheel
Data - Html
Event Converter - 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.
- Rendered
Element Backing - An Element that has been rendered and allows reading and modifying information about it.
Functions§
- onabort
- abort
- onanimationend
- onanimationend
- onanimationiteration
- onanimationiteration
- onanimationstart
- onanimationstart
- onblur
- onblur
- oncanplay
- canplay
- oncanplaythrough
- canplaythrough
- onchange
- onchange
- onclick
- Execute a callback when a button is clicked.
- oncompositionend
- oncompositionend
- oncompositionstart
- oncompositionstart
- oncompositionupdate
- oncompositionupdate
- oncontextmenu
- oncontextmenu
- oncopy
- oncopy
- oncut
- oncut
- ondblclick
Deprecated General Event Handler Information
- ondoubleclick
General Event Handler Information
- ondrag
- ondrag
- ondragend
- ondragend
- ondragenter
- ondragenter
- ondragexit
- ondragexit
- ondragleave
- ondragleave
- ondragover
- ondragover
- ondragstart
- ondragstart
- ondrop
- ondrop
- ondurationchange
- durationchange
- onemptied
- emptied
- onencrypted
- encrypted
- onended
- ended
- onerror
- onerror
- onfocus
- onfocus
- onfocusin
General Event Handler Information
- onfocusout
General Event Handler Information
- ongotpointercapture
- gotpointercapture
- oninput
- The
oninput
event is fired when the value of a<input>
,<select>
, or<textarea>
element is changed. - oninvalid
- oninvalid
- onkeydown
- onkeydown
- onkeypress
- onkeypress
- onkeyup
- onkeyup
- onload
- onload
- onloadeddata
- loadeddata
- onloadedmetadata
- loadedmetadata
- onloadstart
- loadstart
- onlostpointercapture
- lostpointercapture
- onmounted
- The onmounted event is fired when the element is first added to the DOM. This event gives you a
MountedData
object and lets you interact with the raw DOM element. - onmousedown
- onmousedown
- onmouseenter
- onmouseenter
- onmouseleave
- onmouseleave
- onmousemove
- onmousemove
- onmouseout
- onmouseout
- onmouseover
- onmouseover
- onmouseup
- onmouseup
- onpaste
- onpaste
- onpause
- pause
- onplay
- play
- onplaying
- playing
- onpointercancel
- pointercancel
- onpointerdown
- pointerdown
- onpointerenter
- pointerenter
- onpointerleave
- pointerleave
- onpointermove
- pointermove
- onpointerout
- pointerout
- onpointerover
- pointerover
- onpointerup
- pointerup
- onprogress
- progress
- onratechange
- ratechange
- onreset
- onreset
- onresize
- onresize
- onscroll
- onscroll
- onseeked
- seeked
- onseeking
- seeking
- onselect
- select
- onselectionchange
- selectionchange
- onselectstart
- selectstart
- onstalled
- stalled
- onsubmit
- onsubmit
- onsuspend
- suspend
- ontimeupdate
- timeupdate
- ontoggle
- ontoggle
- ontouchcancel
- touchcancel
- ontouchend
- touchend
- ontouchmove
- touchmove
- ontouchstart
- touchstart
- ontransitionend
- transitionend
- onvisible
- onvisible
- onvolumechange
- volumechange
- onwaiting
- waiting
- onwheel
- Called when the mouse wheel is rotated over an element.
- set_
event_ converter
Type Aliases§
- Animation
Event - Clipboard
Event - Composition
Event - Drag
Event - Focus
Event - Form
Event - Image
Event - Keyboard
Event - Media
Event - Mounted
Event - Mounted
Result - The MountedResult type for the MountedData
- Mouse
Event - Pointer
Event - A synthetic event that wraps a web-style
PointerEvent
- Resize
Event - Resize
Result - The ResizeResult type for the ResizeData
- Scroll
Event - Selection
Event - Toggle
Event - Touch
Event - Transition
Event - Visible
Event - Visible
Result - The VisibleResult type for the VisibleData
- Wheel
Event - A synthetic event that wraps a web-style
WheelEvent