0
0
Svelteframework~10 mins

Action return data 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 return data from a Svelte action.

Svelte
function myAction(node) {
  return {
    [1]: { message: 'Hello' }
  };
}
Drag options to blanks, or click blank then click option'
Adata
Bupdate
Cdestroy
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'update' or 'destroy' instead of 'data' to return data.
Not returning an object at all.
2fill in blank
medium

Complete the code to access the returned data from a Svelte action.

Svelte
<div use:myAction let:data>
  <p>[1]</p>
</div>
Drag options to blanks, or click blank then click option'
A{message}
B{data}
C{myAction.data}
D{data.message}
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access data without using let:data.
Using incorrect variable names.
3fill in blank
hard

Fix the error in the action return to properly update data.

Svelte
function myAction(node) {
  return {
    [1]() {
      return { message: 'Updated' };
    }
  };
}
Drag options to blanks, or click blank then click option'
Aupdate
Bdata
Cdestroy
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' as a function instead of a property.
Not returning data from the update function.
4fill in blank
hard

Fill both blanks to return data and define a destroy method in a Svelte action.

Svelte
function myAction(node) {
  return {
    [1]: { count: 0 },
    [2]() {
      console.log('Cleaning up');
    }
  };
}
Drag options to blanks, or click blank then click option'
Adata
Bupdate
Cdestroy
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up update and destroy.
Not returning data properly.
5fill in blank
hard

Fill all three blanks to return data, update it, and destroy the action properly.

Svelte
function myAction(node) {
  let count = 0;
  return {
    [1]: { count },
    [2]() {
      count += 1;
      return { count };
    },
    [3]() {
      console.log('Action destroyed');
    }
  };
}
Drag options to blanks, or click blank then click option'
Adata
Bupdate
Cdestroy
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Not returning updated data from update.
Forgetting to define destroy for cleanup.