Function use_event_with_window

Source
pub fn use_event_with_window<'hook, T, F, E>(
    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 only for window. If you want to specify an event target, use use_event.

§Example

use yew_hooks::prelude::*;

#[function_component(UseEvent)]
fn event() -> Html {
    use_event_with_window("keypress", move |e: KeyboardEvent| {
        debug!("{} is pressed!", e.key());
    });
    
    html! {
        <>
            { "Press any key on your awesome keyboard!" }
        </>
    }
}

§Note

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

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