Custom Actions
All Svelte topics∙ Svelte
Custom Actions explains a use directive function attached to a DOM element with optional update and destroy for this custom, actions lesson. You will learn its exact Svelte rule, failure mode, verification plan, and production evidence.
Syntax
<Card><span slot="title">Title</span></Card>Example
// Topic: Custom Actions
const component = { inputs: 2, events: 1, slots: 1 };
console.log(component.inputs + component.events + component.slots);
// Expected Output: 4Expected Output
4Line-by-line
| Line | Meaning |
|---|---|
const component = { inputs: 2, events: 1, slots: 1 }; | Defines state, behavior, or output for this Svelte example. |
console.log(component.inputs + component.events + component.slots); | Prints the expected result for this Svelte lesson. |
Real-World Uses
- 1Custom Actions is used for design systems, overlays, reusable controls, and feature components.
- 2Its mechanism is a use directive function attached to a DOM element with optional update and destroy for this custom, actions lesson.
- 3Use actions for reusable DOM behavior with explicit parameter and cleanup contracts. Keep decisions specific to custom, actions.
- 4Production code must account for An action that mutates unrelated application state becomes hidden coupling. Do not copy assumptions from a neighboring topic into custom, actions.
- 5Teams evaluate it using DOM behavior reuse and cleanup measured for custom, actions.
Common Mistakes
- 1An action that mutates unrelated application state becomes hidden coupling. Do not copy assumptions from a neighboring topic into custom, actions.
- 2Implementing Custom Actions without understanding a use directive function attached to a DOM element with optional update and destroy for this custom, actions lesson.
- 3Choosing Custom Actions where simpler local Svelte code is clearer.
- 4Skipping Test initialization, parameter update, event dispatch, and destroy. Include an assertion that directly exercises custom, actions.
- 5Optimizing before measuring DOM behavior reuse and cleanup measured for custom, actions.
Best Practices
- 1Use actions for reusable DOM behavior with explicit parameter and cleanup contracts. Keep decisions specific to custom, actions.
- 2Document a use directive function attached to a DOM element with optional update and destroy for this custom, actions lesson in the smallest useful component, store, action, route, or service.
- 3Represent every relevant loading, success, empty, denied, and failure state.
- 4Test initialization, parameter update, event dispatch, and destroy. Include an assertion that directly exercises custom, actions.
- 5Use DOM behavior reuse and cleanup measured for custom, actions to guide improvements.
How it works
- 1Custom Actions relies on a use directive function attached to a DOM element with optional update and destroy for this custom, actions lesson.
- 2Use actions for reusable DOM behavior with explicit parameter and cleanup contracts. Keep decisions specific to custom, actions.
- 3Its main failure mode is An action that mutates unrelated application state becomes hidden coupling. Do not copy assumptions from a neighboring topic into custom, actions.
- 4Useful evidence is DOM behavior reuse and cleanup measured for custom, actions.
Implementation decisions
- 1Identify the owning component, store, action, route, load function, or server handler.
- 2Keep state local until multiple owners genuinely need it.
- 3Keep server secrets and validation outside browser components.
- 4Define cleanup for subscriptions, actions, timers, and requests.
Verification plan
- 1Test initialization, parameter update, event dispatch, and destroy. Include an assertion that directly exercises custom, actions.
- 2Check initial render, assignment-driven updates, user interaction, and cleanup.
- 3Confirm keyboard and screen-reader behavior for visible UI.
- 4Measure production output only after correctness passes.
Practice task
- 1Build the smallest Custom Actions example.
- 2Introduce this failure: An action that mutates unrelated application state becomes hidden coupling. Do not copy assumptions from a neighboring topic into custom, actions.
- 3Correct it using this rule: Use actions for reusable DOM behavior with explicit parameter and cleanup contracts. Keep decisions specific to custom, actions.
- 4Record DOM behavior reuse and cleanup measured for custom, actions before and after the change.
Quick Summary
- Custom Actions works through a use directive function attached to a DOM element with optional update and destroy for this custom, actions lesson.
- Use actions for reusable DOM behavior with explicit parameter and cleanup contracts. Keep decisions specific to custom, actions.
- Avoid An action that mutates unrelated application state becomes hidden coupling. Do not copy assumptions from a neighboring topic into custom, actions.
- Test initialization, parameter update, event dispatch, and destroy. Include an assertion that directly exercises custom, actions.
- Measure success with DOM behavior reuse and cleanup measured for custom, actions.
Interview Questions
Q1. What is Custom Actions used for?
Answer: It is used for design systems, overlays, reusable controls, and feature components.
Q2. How does Custom Actions work in Svelte?
Answer: It works through a use directive function attached to a DOM element with optional update and destroy for this custom, actions lesson.
Q3. What rule matters most?
Answer: Use actions for reusable DOM behavior with explicit parameter and cleanup contracts. Keep decisions specific to custom, actions.
Q4. What failure is common?
Answer: An action that mutates unrelated application state becomes hidden coupling. Do not copy assumptions from a neighboring topic into custom, actions.
Q5. How should it be verified?
Answer: Test initialization, parameter update, event dispatch, and destroy. Include an assertion that directly exercises custom, actions. Evaluate DOM behavior reuse and cleanup measured for custom, actions.
Quiz
Which practice best supports Custom Actions?