0
0
Svelteframework~20 mins

Named actions in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Named Actions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a named action updates its parameter?

Consider a Svelte component using a named action with a parameter. What is the behavior when the parameter changes?

Svelte
<script>
  let color = 'red';
  function highlight(node, params) {
    node.style.backgroundColor = params.color;
    return {
      update(newParams) {
        node.style.backgroundColor = newParams.color;
      },
      destroy() {
        node.style.backgroundColor = null;
      }
    };
  }
</script>

<button use:highlight={{ color }} on:click={() => color = color === 'red' ? 'blue' : 'red'}>Toggle Color</button>
AThe button's background color stays red and never changes.
BThe button's background color toggles between red and blue each time it is clicked.
CThe button's background color changes to blue once and then stops updating.
DThe button's background color flickers rapidly between red and blue.
Attempts:
2 left
💡 Hint

Think about what the update method inside the action does when parameters change.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a named action with a destroy method?

Identify the correct syntax for a named action that cleans up by removing an event listener when destroyed.

A
}
;}  
}    
;)kcilCeldnah ,'kcilc'(renetsiLtnevEevomer.tnemucod      
{ )(yortsed    
{ nruter  
;)kcilCeldnah ,'kcilc'(renetsiLtnevEdda.tnemucod  
;}  
}    
;))'kcilctuo'(tnevEmotsuC wen(tnevEhctapsid.edon      
{ ))tegrat.tneve(sniatnoc.edon!( fi    
{ &gt;= tneve = kcilCeldnah tsnoc  
{ )edon(edistuOkcilc noitcnuf
B
function clickOutside(node) {
  const handleClick = event =&gt; {
    if (!node.contains(event.target)) {
      node.dispatchEvent(new CustomEvent('outclick'));
    }
  };
  document.addEventListener('click', handleClick);
  return {
    destroy() {
      document.removeEventListener('click');
    }
  };
}
C
function clickOutside(node) {
  const handleClick = event =&gt; {
    if (!node.contains(event.target)) {
      node.dispatchEvent(new CustomEvent('outclick'));
    }
  };
  document.addEventListener('click', handleClick);
  return {
    destroy() {
      document.removeEventListener('click', handleClick);
    }
  };
}
D
unction clickOutside(node) {
  const handleClick = event =&gt; {
    if (!node.contains(event.target)) {
      node.dispatchEvent(new CustomEvent('outclick'));
    }
  };
  document.addEventListener('click', handleClick);
  return {
    destroy() {
      document.removeEventListener('click', handleClick);
    }
  };
}
Attempts:
2 left
💡 Hint

Check the syntax of the destroy method and the parameters passed to removeEventListener.

🔧 Debug
advanced
2:00remaining
Why does this named action not update when parameters change?

Given the following named action, why does the background color not update when the parameter changes?

Svelte
function highlight(node, params) {
  node.style.backgroundColor = params.color;
  return {
    destroy() {
      node.style.backgroundColor = null;
    }
  };
}

// Used as: <div use:highlight={{ color }}></div>
ABecause the action does not implement an <code>update</code> method to handle parameter changes.
BBecause the <code>destroy</code> method resets the color immediately after setting it.
CBecause the parameter object is immutable and cannot trigger updates.
DBecause the node style cannot be changed inside named actions.
Attempts:
2 left
💡 Hint

Think about how Svelte named actions handle parameter changes.

state_output
advanced
2:00remaining
What is the final text content after toggling the named action parameter twice?

Consider this Svelte component using a named action that updates text content based on a parameter. What is the final text shown after clicking the button twice?

Svelte
<script>
  let label = 'Hello';
  function updateText(node, params) {
    node.textContent = params.text;
    return {
      update(newParams) {
        node.textContent = newParams.text;
      }
    };
  }
</script>

<button use:updateText={{ text: label }} on:click={() => label = label === 'Hello' ? 'World' : 'Hello'}>Toggle</button>
AAn empty string
BWorld
CHelloWorld
DHello
Attempts:
2 left
💡 Hint

Each click toggles the label. After two clicks, what is the label value?

🧠 Conceptual
expert
3:00remaining
Which statement best describes the lifecycle of a named action in Svelte?

Choose the statement that correctly explains the lifecycle methods and their order in a Svelte named action.

AThe action runs once on mount, then <code>update</code> runs on parameter changes, and finally <code>destroy</code> runs on unmount.
BThe action runs <code>update</code> first, then <code>destroy</code>, and finally the main function on mount.
CThe action runs <code>destroy</code> only if parameters change, otherwise it never runs.
DThe action runs <code>destroy</code> on mount, then <code>update</code> on unmount, and the main function never runs.
Attempts:
2 left
💡 Hint

Recall the order of lifecycle methods: mount, update, destroy.