javascript / beginner
Snippet
Action Directives
Actions are functions that run when an element is created. They allow you to interface with the DOM directly and encapsulate reusable logic for specific elements.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
<script>function focusOnLoad(node) {node.focus();return {destroy() { console.log('Node removed'); }};}</script><input use:focusOnLoad />
svelte
Breakdown
1
function focusOnLoad(node) { ... }
A function that receives the DOM node as an argument when the element enters the DOM.
2
use:focusOnLoad
The 'use' directive applies the action function to this specific element.