dioxus/reference/listener.rs

57 lines
1.5 KiB
Rust
Raw Normal View History

2021-07-02 01:30:52 -04:00
//! Example: Listeners
//! ------------------
//!
//! This example demonstrates the various ways listeners may be used in Dioxus.
//! Listeners may be at most `FnMut` - but not `FnOnce`.
//! Listeners may borrow the lifetime of cx (children and hooks), but not of local (stack) data.
use dioxus::prelude::*;
2021-09-21 13:42:52 -04:00
pub static Example: FC<()> = |cx, props| {
2021-07-16 16:11:25 -04:00
cx.render(rsx! {
ButtonList {}
NonUpdatingEvents {}
DisablePropogation {}
})
};
2021-07-02 01:30:52 -04:00
/// We can use `set_name` in multiple closures; the closures automatically *copy* the reference to set_name.
2021-09-21 13:42:52 -04:00
static ButtonList: FC<()> = |cx, props| {
2021-07-11 18:39:45 -04:00
let name = use_state(cx, || "...?");
2021-07-02 01:30:52 -04:00
let names = ["jack", "jill", "john", "jane"]
.iter()
2021-07-11 18:39:45 -04:00
.map(move |n| rsx!(button { onclick: move |_| name.set(n), "{n}" }));
2021-07-02 01:30:52 -04:00
cx.render(rsx!(
div {
h1 { "Hello, {name}" }
{names}
}
))
};
/// This shows how listeners may be without a visible change in the display.
/// Check the console.
2021-09-21 13:42:52 -04:00
static NonUpdatingEvents: FC<()> = |cx, props| {
2021-09-16 13:20:04 -04:00
rsx!(cx, div {
2021-07-02 01:30:52 -04:00
button {
onclick: move |_| log::info!("Did not cause any updates!")
"Click me to log!"
}
})
};
2021-09-21 13:42:52 -04:00
static DisablePropogation: FC<()> = |cx, props| {
2021-09-16 13:20:04 -04:00
rsx!(cx,
2021-07-02 01:30:52 -04:00
div {
onclick: move |_| log::info!("event propogated to the div!")
button {
onclick: move |evt| {
log::info!("Button will allow propogation");
}
}
}
)
};