0
0
Svelteframework~20 mins

Action update and destroy 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 happens when an action's update function changes the element's style?

Consider this Svelte action that updates the background color of an element when the parameter changes:

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

If this action is used on a <div> with initial color 'yellow' and later updated to 'lightblue', what will be the background color after update?

AThe background color will be 'yellow' because update does not change it.
BThe background color will be 'yellow' until the element is destroyed.
CThe background color will be removed (empty) after update.
DThe background color will be 'lightblue' as the update function sets it.
Attempts:
2 left
💡 Hint

Think about what the update function does when called with a new parameter.

lifecycle
intermediate
2:00remaining
When is the destroy function of a Svelte action called?

Given a Svelte action with a destroy method, when does Svelte call this method?

AWhen the element the action is attached to is removed from the DOM.
BEvery time the action's parameter updates.
CWhen the component using the action is first created.
DWhen the browser window is resized.
Attempts:
2 left
💡 Hint

Think about cleanup tasks and when they should happen.

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

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

Afunction action(node) { return { update() {}, destroy() {} } }
Bfunction action(node) { update() {}, destroy() {} }
Cfunction action(node) { return update() {}, destroy() {} }
Dfunction action(node) { return { update() {}, destroy() } }
Attempts:
2 left
💡 Hint

Remember the action must return an object with methods.

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

Look at this action:

function example(node, param) {
  console.log('init', param);
  return {
    update(newParam) {
      console.log('update', newParam);
    }
  };
}

When the parameter changes, the 'update' log never appears. What is the likely cause?

AThe console.log inside update is incorrect syntax.
BThe update method is missing a return statement.
CThe action was not re-applied with the new parameter, so update is not called.
DSvelte actions do not support update methods.
Attempts:
2 left
💡 Hint

Think about how Svelte knows to call update.

state_output
expert
3:00remaining
What is the final text content after action update and destroy?

Consider this Svelte action and usage:

function textAction(node, text) {
  node.textContent = text;
  return {
    update(newText) {
      node.textContent = newText;
    },
    destroy() {
      node.textContent = 'Removed';
    }
  };
}

// Usage in a component:
// <div use:textAction={message}></div>

// Initial message = 'Hello'
// Then message changes to 'World'
// Then the div is removed from DOM

What is the text content of the div at each stage?

AInitially empty, then 'Hello', then 'World' after removal.
BInitially 'Hello', then 'World', then 'Removed' after removal.
CAlways 'Hello' regardless of updates or removal.
DInitially 'Hello', then 'World', then empty after removal.
Attempts:
2 left
💡 Hint

Remember what update and destroy do to the text content.