isActive is true?Consider this Svelte snippet:
<script>
let isActive = true;
</script>
<div class:active={isActive}>Hello</div>Which class will the <div> have when isActive is true?
<script> let isActive = true; </script> <div class:active={isActive}>Hello</div>
Think about how class:name={condition} works in Svelte.
When isActive is true, the class active is added to the element.
isError?Given this Svelte component:
<script>
let isError = false;
function toggle() {
isError = !isError;
}
</script>
<button on:click={toggle}>Toggle Error</button>
<div class:error={isError} class:ok={!isError}>Status</div>What classes does the <div> have after clicking the button once?
<script> let isError = false; function toggle() { isError = !isError; } </script> <button on:click={toggle}>Toggle Error</button> <div class:error={isError} class:ok={!isError}>Status</div>
Clicking toggles isError from false to true.
Initially, isError is false, so the div has class ok. After toggling once, isError becomes true, so the div has class error only.
Identify the option that will cause a syntax error in Svelte when using conditional classes.
Check the syntax for binding class names conditionally in Svelte.
Option B misses the curly braces around the expression, causing a syntax error. The correct syntax requires curly braces for the condition.
Consider this Svelte code:
<script>
let isVisible = false;
function toggle() {
isVisible == !isVisible;
}
</script>
<button on:click={toggle}>Toggle</button>
<div class:visible={isVisible}>Show me</div>Why does clicking the button not toggle the visible class?
<script> let isVisible = false; function toggle() { isVisible == !isVisible; } </script> <button on:click={toggle}>Toggle</button> <div class:visible={isVisible}>Show me</div>
Look carefully at the assignment inside the toggle function.
The toggle function uses '==' which is a comparison operator, not assignment. It should use '=' to update the variable.
Given this Svelte snippet:
<script>
let isRed = true;
let isBlue = false;
let isGreen = true;
</script>
<div class:red={isRed} class:blue={isBlue} class:green={isGreen}>Colors</div>What classes will the <div> have when rendered?
<script> let isRed = true; let isBlue = false; let isGreen = true; </script> <div class:red={isRed} class:blue={isBlue} class:green={isGreen}>Colors</div>
Only classes with true conditions are applied.
Since isRed and isGreen are true, their classes are added. isBlue is false, so 'blue' is not added.