Function use_event

Source
pub fn use_event<'hook, T, F, E>(
    node: NodeRef,
    event_type: T,
    callback: F,
) -> impl 'hook + Hook<Output = ()>
where T: Into<Cow<'static, str>> + 'hook, F: Fn(E) + 'static + 'hook, E: From<JsValue> + 'hook,
Expand description

A hook that subscribes a callback to events.

§Example

use yew_hooks::prelude::*;

#[function_component(UseEvent)]
fn event() -> Html {
    let button = use_node_ref();

    use_event(button.clone(), "click", move |_: MouseEvent| {
        debug!("Clicked!");
    });
    
    html! {
        <>
            <button ref={button}>{ "Click me!" }</button>
        </>
    }
}

§Note

When used in function components and hooks, this hook is equivalent to:

pub fn use_event<T, F, E>(node: NodeRef, event_type: T, callback: F)
where
    T: Into<Cow<'static, str>>,
    F: Fn(E) + 'static,
    E: From<JsValue>,
{
    /* implementation omitted */
}