Bird
Raised Fist0
Svelteframework~10 mins

Named actions in Svelte - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to apply a named action called 'focus' to the input element.

Svelte
<input use:[1] />
Drag options to blanks, or click blank then click option'
Ahover
Bclick
Cfocus
Dscroll
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use the 'use:' prefix before the action name.
Using event names like 'click' instead of action names.
2fill in blank
medium

Complete the code to define a named action 'highlight' that changes the background color of an element.

Svelte
function [1](node) {
  node.style.backgroundColor = 'yellow';
  return {
    destroy() {
      node.style.backgroundColor = '';
    }
  };
}
Drag options to blanks, or click blank then click option'
Ahighlight
Bclick
Cfocus
Dhover
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the function differently than the action used in the template.
Not returning a destroy method to clean up.
3fill in blank
hard

Fix the error in the named action usage by completing the code to pass a parameter 'color' to the action.

Svelte
<script>
  function highlight(node, [1]) {
    node.style.backgroundColor = color;
    return {
      update(newColor) {
        node.style.backgroundColor = newColor;
      },
      destroy() {
        node.style.backgroundColor = '';
      }
    };
  }
</script>

<div use:highlight={'yellow'}>Highlight me</div>
Drag options to blanks, or click blank then click option'
Aevent
Bcolor
Cnode
Dstyle
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parameter name that does not match the passed argument.
Not defining the second parameter to receive the value.
4fill in blank
hard

Fill both blanks to create a named action 'tooltip' that adds a title attribute and updates it when the parameter changes.

Svelte
function [1](node, [2]) {
  node.title = text;
  return {
    update(newText) {
      node.title = newText;
    },
    destroy() {
      node.title = '';
    }
  };
}
Drag options to blanks, or click blank then click option'
Atooltip
Bcolor
Ctext
Dhighlight
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent parameter names that don't match usage.
Not returning an update method to handle parameter changes.
5fill in blank
hard

Fill all three blanks to create a named action 'resize' that listens for window resize events and calls a callback parameter.

Svelte
<script>
  function [1](node, [2]) {
    function onResize() {
      callback();
    }
    window.addEventListener('resize', onResize);
    return {
      destroy() {
        window.removeEventListener('resize', [3]);
      }
    };
  }
</script>
Drag options to blanks, or click blank then click option'
Aresize
Bcallback
ConResize
DhandleResize
Attempts:
3 left
💡 Hint
Common Mistakes
Removing the event listener with a different function name than added.
Not passing the callback parameter correctly.

Practice

(1/5)
1. What is the main purpose of named actions in Svelte?
easy
A. To style elements with CSS
B. To define new components
C. To add reusable behavior to HTML elements
D. To manage application state

Solution

  1. Step 1: Understand what named actions do

    Named actions in Svelte are used to add behavior to HTML elements that can be reused across components.
  2. Step 2: Compare with other options

    Defining components, styling, and state management are handled differently in Svelte, not by named actions.
  3. Final Answer:

    To add reusable behavior to HTML elements -> Option C
  4. Quick Check:

    Named actions = reusable element behavior [OK]
Hint: Named actions add behavior to elements, not styles or state [OK]
Common Mistakes:
  • Confusing actions with components
  • Thinking actions handle styling
  • Assuming actions manage state
2. Which of the following is the correct syntax to define a named action in Svelte?
easy
A. function myAction(node) { /* code */ }
B. const myAction = () => { /* code */ }
C. let myAction = node => { /* code */ }
D. action myAction(node) { /* code */ }

Solution

  1. Step 1: Recall named action syntax

    Named actions are functions that receive the element node as the first argument, typically declared as function myAction(node) {}.
  2. Step 2: Check other options

    Arrow functions can work but must return an object for cleanup; action myAction(node) { /* code */ } uses invalid syntax.
  3. Final Answer:

    function myAction(node) { /* code */ } -> Option A
  4. Quick Check:

    Named action = function with node parameter [OK]
Hint: Named actions are functions with node parameter, not keywords [OK]
Common Mistakes:
  • Using invalid 'action' keyword
  • Defining without node parameter
  • Using arrow function without return object
3. Given this named action and usage:
function highlight(node) {
  node.style.backgroundColor = 'yellow';
  return {
    destroy() {
      node.style.backgroundColor = '';
    }
  };
}

<div use:highlight>Hello</div>

What happens when the component is removed from the page?
medium
A. The background color resets to default
B. The element is removed from the DOM immediately
C. An error occurs because destroy is missing
D. The background color stays yellow

Solution

  1. Step 1: Understand the action's destroy method

    The destroy method resets the background color to empty string when the element is removed.
  2. Step 2: Effect on component removal

    When the component unmounts, destroy runs, so the background color resets to default.
  3. Final Answer:

    The background color resets to default -> Option A
  4. Quick Check:

    destroy resets styles on removal [OK]
Hint: Destroy cleans up styles when element is removed [OK]
Common Mistakes:
  • Assuming styles persist after removal
  • Thinking destroy is optional
  • Confusing destroy with element removal
4. What is wrong with this named action code?
function focus(node) {
  node.focus();
  return {
    destroy() {
      node.blur();
    }
  }
}

<input use:focus>
medium
A. Calling node.blur() in destroy may cause errors if node is removed
B. The action should not call node.focus() immediately
C. The action does not return an object
D. The action must be async to work properly

Solution

  1. Step 1: Analyze the focus call timing

    Calling node.focus() immediately may not work reliably because the element might not be fully rendered, visible, or ready (e.g., during CSS transitions or animations).
  2. Step 2: Best practice for focus actions

    In Svelte, it's often necessary to delay the focus call using tick(), requestAnimationFrame, or setTimeout(..., 0) to ensure DOM readiness.
  3. Final Answer:

    The action should not call node.focus() immediately -> Option B
  4. Quick Check:

    Delay focus with tick() or RAF [OK]
Hint: Delay node.focus() with tick() for reliability [OK]
Common Mistakes:
  • Calling focus immediately without delay
  • Ignoring DOM/transition timing issues
  • Assuming focus always succeeds synchronously
5. You want to create a named action that changes an element's text color based on a parameter and updates it when the parameter changes. Which approach is correct?
hard
A. Use a reactive statement inside the action function to watch parameters
B. Set the color once in the action and ignore parameter changes
C. Recreate the action every time parameters change instead of updating
D. Return an update function from the action that sets the color when parameters change

Solution

  1. Step 1: Understand parameter updates in named actions

    Named actions can return an update function to handle parameter changes dynamically.
  2. Step 2: Evaluate options for handling parameter changes

    Setting color once ignores updates; reactive statements inside action function don't track parameters; recreating action is inefficient.
  3. Final Answer:

    Return an update function from the action that sets the color when parameters change -> Option D
  4. Quick Check:

    Use update() to handle parameter changes [OK]
Hint: Use update() function to react to parameter changes [OK]
Common Mistakes:
  • Ignoring parameter updates
  • Misusing reactive statements inside actions
  • Recreating actions unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard