Skip to main content
InterviewHubPRO
Section Overview
Q12

Question 12 of 50

Part 1: Basic & Foundational Concepts (Junior / Entry-Level)

24%
Svelte & SvelteKit (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)

Svelte Actions (`use:action`): Direct DOM Node Lifecycle Management.

junior LevelCode ExampleState ManagementLifecycleSvelte 5

In Simple Words: An Action is a function invoked when a DOM element is mounted. It receives the DOM node as an argument:

svelte
<script>
  function clickOutside(node, callback) {
    const handleClick = (e) => {
      if (!node.contains(e.target)) callback();
    };
    document.addEventListener('click', handleClick);

    return {
      destroy() {
        document.removeEventListener('click', handleClick);
      }
    };
  }

  let showModal = $state(true);
</script>

<div use:clickOutside={() => showModal = false}>
  Modal Content
</div>