0
0
Svelteframework~20 mins

Action return data in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Action Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Svelte action return data affect?

Consider this Svelte action that returns an object with a destroy method and a update method. What is the main purpose of returning these methods from an action?

Svelte
function highlight(node, color) {
  node.style.backgroundColor = color;
  return {
    update(newColor) {
      node.style.backgroundColor = newColor;
    },
    destroy() {
      node.style.backgroundColor = '';
    }
  };
}
AThey specify CSS styles that the action applies permanently to the node.
BThey define event listeners that the action automatically attaches to the node.
CThey allow the action to respond to parameter changes and clean up when the element is removed.
DThey enable the action to return data to the parent component synchronously.
Attempts:
2 left
💡 Hint

Think about what happens when the action's parameters change or when the element is removed from the page.

state_output
intermediate
2:00remaining
What is the output of this Svelte action's return data usage?

Given this Svelte action and component usage, what will be the background color of the div after 2 seconds?

Svelte
function colorChange(node) {
  node.style.backgroundColor = 'red';
  const timeout = setTimeout(() => {
    node.style.backgroundColor = 'blue';
  }, 1000);
  return {
    destroy() {
      clearTimeout(timeout);
    }
  };
}

<!-- Component -->
<div use:colorChange></div>
AThe background color will remain red after 2 seconds.
BThe background color will be blue after 2 seconds.
CThe background color will be removed after 2 seconds.
DThe code will throw an error because <code>use:colorChange</code> is invalid.
Attempts:
2 left
💡 Hint

Look at the setTimeout inside the action and what it does after 1 second.

🔧 Debug
advanced
2:00remaining
Why does this Svelte action's update method not run on parameter change?

Examine this Svelte action and usage. Why does the update method never get called when the parameter changes?

Svelte
function toggle(node, active) {
  node.style.opacity = active ? '1' : '0.5';
  return {
    update(newActive) {
      node.style.opacity = newActive ? '1' : '0.5';
    }
  };
}

<!-- Component -->
<script>
  let isActive = true;
  setTimeout(() => isActive = false, 1000);
</script>

<div use:toggle={isActive}></div>
ABecause the action parameter is not reactive; the action must be recreated to call update.
BBecause the action is not returning an object, so update cannot be called.
CBecause the action is missing a <code>destroy</code> method, so update is ignored.
DBecause the action is used incorrectly; <code>use:toggle</code> cannot accept parameters.
Attempts:
2 left
💡 Hint

Think about how Svelte calls the update method on actions when parameters change.

📝 Syntax
advanced
2:00remaining
Which option correctly returns an action with update and destroy methods?

Choose the correct Svelte action syntax that returns both update and destroy methods.

A
function action(node) {
  return {
    update: function(param) {
      node.textContent = param;
    },
    destroy: function() {
      node.textContent = '';
    }
  };
}
B
function action(node) {
  update(param) {
    node.textContent = param;
  }
  destroy() {
    node.textContent = '';
  }
}
C
function action(node) {
  return update(param) {
    node.textContent = param;
  },
  destroy() {
    node.textContent = '';
  }
}
D
function action(node) {
  return {
    update(param) {
      node.textContent = param;
    },
    destroy() {
      node.textContent = '';
    }
  };
}
Attempts:
2 left
💡 Hint

Remember the object literal syntax for returning methods in JavaScript.

🧠 Conceptual
expert
3:00remaining
How does Svelte use the return data from an action internally?

When a Svelte action returns an object with update and destroy methods, how does Svelte use these methods during the component lifecycle?

ASvelte calls <code>update</code> whenever the action's parameter changes and calls <code>destroy</code> when the element is removed from the DOM.
BSvelte calls <code>update</code> only once after the action is first applied and never calls <code>destroy</code>.
CSvelte calls <code>destroy</code> before calling <code>update</code> on every parameter change.
DSvelte ignores the returned object and manages the action lifecycle internally without calling these methods.
Attempts:
2 left
💡 Hint

Think about when parameters change and when elements are removed in Svelte.