0
0
Svelteframework~20 mins

Action parameters in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Action Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when using an action with parameters in Svelte?
Consider this Svelte action that changes the background color of an element based on a parameter. What will be the background color of the
after mounting?

function colorAction(node, color) {
  node.style.backgroundColor = color;
  return {
    update(newColor) {
      node.style.backgroundColor = newColor;
    }
  };
}

<div use:colorAction={'red'}>Hello</div>
Svelte
function colorAction(node, color) {
  node.style.backgroundColor = color;
  return {
    update(newColor) {
      node.style.backgroundColor = newColor;
    }
  };
}

<div use:colorAction={'red'}>Hello</div>
AThe div will have a background color of 'undefined'.
BThe div will have no background color because the action parameter is ignored.
CThe div will have a default background color set by the browser.
DThe div will have a red background color.
Attempts:
2 left
💡 Hint
Remember that the action receives the parameter as the second argument and applies it immediately.
state_output
intermediate
2:00remaining
How does updating action parameters affect the DOM in Svelte?
Given this Svelte action and component, what will be the background color of the
after the button is clicked?

function colorAction(node, color) {
  node.style.backgroundColor = color;
  return {
    update(newColor) {
      node.style.backgroundColor = newColor;
    }
  };
}

<script>
  let bg = 'blue';
</script>

<div use:colorAction={bg}>Color me</div>
<button on:click={() => bg = 'green'}>Change Color</button>
Svelte
function colorAction(node, color) {
  node.style.backgroundColor = color;
  return {
    update(newColor) {
      node.style.backgroundColor = newColor;
    }
  };
}

<script>
  let bg = 'blue';
</script>

<div use:colorAction={bg}>Color me</div>
<button on:click={() => bg = 'green'}>Change Color</button>
AThe div's background color stays blue even after clicking the button.
BThe div's background color changes from blue to green after clicking the button.
CThe div's background color changes to red after clicking the button.
DThe div's background color becomes transparent after clicking the button.
Attempts:
2 left
💡 Hint
Check how the action's update method reacts to parameter changes.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a Svelte action with parameters and cleanup?
You want to create a Svelte action that logs a message when the element is mounted and removes the event listener on destroy. Which code snippet is correct?
A
function logAction(node, message) {
  console.log(message);
  const handler = () =&gt; console.log('clicked');
  node.addEventListener('click', handler);
  return {
    destroy() {
      node.removeEventListener('click', handler);
    }
  };
}
B
function logAction(node, message) {
  console.log(message);
  const handler = () =&gt; console.log('clicked');
  node.addEventListener('click', handler);
  return {
    update(newMessage) {
      console.log(newMessage);
    }
  };
}
C
function logAction(node, message) {
  console.log(message);
  node.addEventListener('click', () =&gt; console.log('clicked'));
  return {
    destroy() {
      node.removeEventListener('click');
    }
  };
}
D
function logAction(node, message) {
  console.log(message);
  node.addEventListener('click', () =&gt; console.log('clicked'));
  return {};
}
Attempts:
2 left
💡 Hint
Remember to keep a reference to the event handler to remove it later.
🔧 Debug
advanced
2:00remaining
Why does this Svelte action not update when parameters change?
Examine this Svelte action usage:

function borderAction(node, width) {
  node.style.border = width + 'px solid black';
}

<script>
  let borderWidth = 2;
</script>

<div use:borderAction={borderWidth}>Box</div>
<button on:click={() => borderWidth = 5}>Increase Border</button>

Why does the border not update when clicking the button?
Svelte
function borderAction(node, width) {
  node.style.border = width + 'px solid black';
}

<script>
  let borderWidth = 2;
</script>

<div use:borderAction={borderWidth}>Box</div>
<button on:click={() => borderWidth = 5}>Increase Border</button>
AThe variable borderWidth is not reactive.
BThe border style is invalid and ignored by the browser.
CThe action lacks an update method to handle parameter changes.
DThe action is not applied because of a syntax error.
Attempts:
2 left
💡 Hint
Check if the action handles changes to its parameters after initial mount.
🧠 Conceptual
expert
3:00remaining
What happens if an action's update method returns a new destroy function in Svelte?
In Svelte, an action can return an object with update and destroy methods. What is the effect if the update method returns a new destroy function?

Consider:
function exampleAction(node, param) {
  const cleanup = () => console.log('cleanup old');
  node.style.color = param;
  return {
    update(newParam) {
      cleanup();
      node.style.color = newParam;
      return () => console.log('cleanup new');
    },
    destroy() {
      console.log('destroy called');
    }
  };
}
Svelte
function exampleAction(node, param) {
  const cleanup = () => console.log('cleanup old');
  node.style.color = param;
  return {
    update(newParam) {
      cleanup();
      node.style.color = newParam;
      return () => console.log('cleanup new');
    },
    destroy() {
      console.log('destroy called');
    }
  };
}
AThe returned function from update replaces the previous destroy function and is called before the next update or destroy.
BThe returned function from update is called immediately after update finishes.
CSvelte throws an error because update cannot return a function.
DThe returned function from update is ignored; only the original destroy is called on unmount.
Attempts:
2 left
💡 Hint
Think about how Svelte manages cleanup between updates and unmount.