0
0
Svelteframework~10 mins

Action parameters in Svelte - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to pass a parameter to the action function.

Svelte
<script>
  function highlight(node, [1]) {
    node.style.backgroundColor = [1].color;
    return {
      destroy() {
        node.style.backgroundColor = '';
      }
    };
  }
</script>

<p use:highlight={{ color: 'yellow' }}>Highlight me!</p>
Drag options to blanks, or click blank then click option'
Aparams
Bcolor
Cnode
Devent
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parameter name instead of the parameters object.
Confusing the node with the parameters.
2fill in blank
medium

Complete the code to update the action when parameters change.

Svelte
<script>
  function highlight(node, params) {
    node.style.backgroundColor = params.color;
    return {
      [1](newParams) {
        node.style.backgroundColor = newParams.color;
      }
    };
  }
</script>
Drag options to blanks, or click blank then click option'
Aupdate
Bchange
Crefresh
Dmodify
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist in Svelte actions.
Not implementing the update method to handle parameter changes.
3fill in blank
hard

Fix the error in the action function parameter usage.

Svelte
<script>
  function tooltip([1]) {
    // code to show tooltip
  }
</script>
Drag options to blanks, or click blank then click option'
Aparams, node
Bnode, params
Cnode
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of node and params.
Using only params without the node.
4fill in blank
hard

Fill both blanks to correctly destructure parameters in the action function.

Svelte
<script>
  function focus(node, [1]) {
    const { delay, color } = [2];
    setTimeout(() => {
      node.style.outlineColor = color;
      node.focus();
    }, delay);
  }
</script>
Drag options to blanks, or click blank then click option'
Aparams
Boptions
Dsettings
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the parameter object and destructuring from another variable.
Not destructuring the parameter object correctly.
5fill in blank
hard

Fill all three blanks to correctly implement an action with parameters and update method.

Svelte
<script>
  function resize(node, [1]) {
    function applySize() {
      node.style.width = [2] + 'px';
      node.style.height = [3] + 'px';
    }
    applySize();
    return {
      update(newParams) {
        [1] = newParams;
        applySize();
      }
    };
  }
</script>
Drag options to blanks, or click blank then click option'
Aparams
Bparams.width
Cparams.height
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Not updating the params variable inside the update method.
Using wrong variable names for width and height.