Skip to main content

use_window_event

Function use_window_event 

Source
pub fn use_window_event<F>(event_name: &str, callback: F)
where F: FnMut() + 'static,
Expand description

Registers a window.addEventListener callback using event delegation, automatically removed when the hook context is cleared.

Uses the global window event proxy registry so that only one window.addEventListener call is made per event name regardless of how many components listen to the same event. On cleanup, only the handler entry is removed from the proxy registry; the shared window listener remains active for other consumers.

The event listener is only registered once on the first render. On subsequent re-renders at the same hook index, this is a no-op.

§Arguments

  • &str - The event name to listen for (e.g., “hashchange”, “popstate”, “resize”).
  • FnMut() + 'static - The callback to invoke when the event fires.