Svelte · Junior · Question 46 of 76
How do you add a listener to window from a component?
- ✓Correct answer:
<svelte:window onkeydown={handleKey} /> - 2
window.addEventListenerat the top level of<script> - 3
<window onkeydown={handleKey}>
Explanation
Svelte adds and removes the listener with the component lifecycle and it's safe during SSR. You can also bind innerWidth, scrollY and similar values.