0
0
Svelteframework~20 mins

Action with event dispatching in Svelte - Practice Problems & Coding Challenges

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

Consider this Svelte action that dispatches a custom event when the element is clicked.

function clickAction(node) {
  const handleClick = () => {
    node.dispatchEvent(new CustomEvent('customclick', { detail: { clicked: true } }));
  };
  node.addEventListener('click', handleClick);
  return {
    destroy() {
      node.removeEventListener('click', handleClick);
    }
  };
}

Which event name should you listen for in the component using this action?

AclickAction
Bcustomclick
Cclick
Dactionclick
Attempts:
2 left
💡 Hint

Look at the event name passed to CustomEvent.

state_output
intermediate
2:00remaining
What is the output after dispatching an event in a Svelte action?

Given this Svelte component using an action that dispatches an event:

<script>
  let message = '';
  function myAction(node) {
    const sendEvent = () => {
      node.dispatchEvent(new CustomEvent('notify', { detail: 'hello' }));
    };
    sendEvent();
    return {};
  }
</script>

<div use:myAction on:notify={e => message = e.detail}>Click me</div>
<p>{message}</p>

What will be shown inside the <p> tag when the component renders?

Aundefined
B'' (empty string)
C'hello'
DError: event handler not found
Attempts:
2 left
💡 Hint

The action dispatches the event immediately on mount.

📝 Syntax
advanced
2:30remaining
Which Svelte action syntax correctly dispatches a custom event?

Which of the following Svelte action implementations correctly dispatches a custom event named 'update' with detail { value: 42 } when the element is clicked?

A
function action(node) {
  const handler = () =&gt; node.dispatchEvent(new CustomEvent('update', { detail: { value: 42 } }));
  node.addEventListener('click', handler);
  return { destroy() { node.removeEventListener('click', handler); } };
}
B
function action(node) {
  node.dispatchEvent(new CustomEvent('update', { detail: { value: 42 } }));
  return {};
}
C
function action(node) {
  node.addEventListener('click', () =&gt; {
    node.dispatchEvent('update', { value: 42 });
  });
  return { destroy() { node.removeEventListener('click'); } };
}
D
function action(node) {
  node.addEventListener('click', () =&gt; {
    node.dispatchEvent(new CustomEvent('update', { detail: { value: 42 } }));
  });
  return { destroy() { node.removeEventListener('click'); } };
}
Attempts:
2 left
💡 Hint

Check that event listeners are properly added and removed, and that CustomEvent is used correctly.

🔧 Debug
advanced
2:30remaining
Why does this Svelte action's event not trigger the handler?

Given this Svelte action:

function action(node) {
  node.addEventListener('click', () => {
    node.dispatchEvent(new CustomEvent('change', { detail: 123 }));
  });
  return {};
}

And this usage in a component:

<div use:action on:change={e => console.log(e.detail)}>Click me</div>

Clicking the div does not log anything. Why?

AThe event 'change' is a native DOM event and cannot be dispatched as a CustomEvent.
BThe action does not remove the event listener on destroy, causing a memory leak but not blocking events.
CThe event bubbles by default, so the handler should catch it; the problem is elsewhere.
DThe event is dispatched on the same node, but the event listener is on the node itself, so it should work; the issue is that the event is not set to bubble.
Attempts:
2 left
💡 Hint

Check if the custom event bubbles by default.

🧠 Conceptual
expert
3:00remaining
How to correctly forward a custom event from a Svelte action to a component?

You want a Svelte action to dispatch a custom event that the component using the action can listen to with on:customEvent. Which approach ensures the event is properly received by the component?

ADispatch the event on the node with <code>node.dispatchEvent(new CustomEvent('customEvent'))</code> and ensure the event bubbles by setting <code>bubbles: true</code>.
BDispatch the event on the document object instead of the node to guarantee the component catches it.
CUse <code>window.dispatchEvent</code> to send the event globally and listen with <code>on:customEvent</code> in the component.
DDispatch the event on the node without bubbles and listen on the parent element of the component.
Attempts:
2 left
💡 Hint

Think about event propagation and where the component listens for events.