Performance: Event modifiers (preventDefault, stopPropagation)
This concept affects how quickly user interactions are processed and how many unnecessary event handlers run, impacting input responsiveness and visual stability.
<button on:click|preventDefault|stopPropagation={handleClick}>Click me</button>
<script>
function handleClick() {
// other logic
}
</script><button on:click={handleClick}>Click me</button>
<script>
function handleClick(event) {
event.preventDefault();
event.stopPropagation();
// other logic
}
</script>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual preventDefault and stopPropagation in handler | No extra DOM ops | 0 reflows | Minimal paint | [!OK] |
| Svelte event modifiers (|preventDefault|stopPropagation) | No extra DOM ops | 0 reflows | Minimal paint | [OK] Good |
preventDefault event modifier do in Svelte?preventDefault modifier stops the browser's default behavior, like following a link or submitting a form.stopPropagation stops event bubbling, not default actions. The other options describe unrelated behaviors.| to add modifiers like preventDefault after the event name.on:submit|preventDefault. uses wrong modifier, C uses dot instead of pipe, D uses incomplete modifier.<button on:click|stopPropagation={handleClick}>Click me</button>
function handleClick() {
alert('Button clicked');
}
document.body.addEventListener('click', () => alert('Body clicked'));stopPropagation modifier stops the event from bubbling up to parent elements like document.body.<a href='https://example.com' on:click|preventDefault|stopPropagation={goSomewhere}>Link</a>
function goSomewhere() {
console.log('Navigating');
}preventDefault and stopPropagation modifiers automatically call the respective event methods before the handler runs.event.preventDefault()</> manually.<ul on:click={parentClick}>
<li on:click|???={childClick}>Child</li>
</ul>
function parentClick() {
console.log('Parent clicked');
}
function childClick() {
console.log('Child clicked');
}stopPropagation on the child's click event stops the event from bubbling to the parent, preventing parent's handler from running.preventDefault is unnecessary.Pass the test and this pattern moves unknown