4. Identify the error in this Svelte code that prevents the transition from working:
<script>
import { fade } from 'svelte/transition';
let show = true;
</script>
{#if show}
<div transition=fade>Content</div>
{/if}
<button on:click={() => show = !show}>Toggle</button>
medium
A. The transition directive syntax is incorrect; it should be transition:fade.
B. The variable 'show' is not initialized properly.
C. The import statement for fade is missing.
D. The button click handler syntax is invalid.
Solution
Step 1: Check transition directive syntax
The correct syntax uses a colon: transition:fade, not transition=fade.
Step 2: Verify other parts
The variable 'show' is initialized, fade is imported, and button syntax is valid.
Final Answer:
The transition directive syntax is incorrect; it should be transition:fade. -> Option A
Quick Check:
Correct transition syntax uses colon = B [OK]
Hint: Use colon in transition directive: transition:fade [OK]
Common Mistakes:
Using '=' instead of ':' in transition directive
Assuming variable initialization is wrong
Missing import statement
Incorrect button event syntax
5. You want to add a slide transition that lasts 500ms when a component appears or disappears. Which Svelte code snippet correctly applies this with accessibility in mind?