false, what is the effect on the event's default behavior?<script> function preventDefaultAction(node) { node.addEventListener('click', event => { event.preventDefault(); }); return false; } </script> <button use:preventDefaultAction on:click={() => alert('Clicked!')}>Click me</button>
In Svelte, returning false from a default action does not automatically prevent the event's default behavior. You must explicitly call event.preventDefault() inside the event listener. Returning false alone has no effect.
highlight that adds a yellow background to an element.Default actions in Svelte must be exported functions so they can be used with use:. Option C correctly exports the function. Option B is not exported, so it won't work. Option A exports a default function, which is not how default actions are imported. Option D exports a constant function, which is valid syntax and recognized as an action.
export function trackClicks(node) { function onClick() { console.log('Clicked'); } node.addEventListener('click', onClick); }
Svelte expects default actions to return an object with a destroy method that cleans up any side effects like event listeners. Without this, the event listener remains attached even after the element is removed, causing memory leaks or unexpected behavior.
<script> import { onMount } from 'svelte'; let count = 0; export function increment(node) { function onClick() { count += 1; } node.addEventListener('click', onClick); return { destroy() { node.removeEventListener('click', onClick); } }; } </script> <button use:increment>Clicked {count} times</button>
The default action runs outside Svelte's reactivity system. Updating count inside the action does not trigger reactive updates because count is not in scope inside the action function. The displayed count remains 0.
Default actions allow you to run code when an element is created and clean up when it is destroyed. Inline event handlers only respond to events but do not manage setup or teardown. This makes default actions ideal for behaviors that need lifecycle management.