0
0
Svelteframework~20 mins

Readonly props in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Readonly Props Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a child tries to modify a readonly prop in Svelte?

Consider a Svelte component that receives a prop. What will happen if the child component tries to change that prop's value?

Svelte
<script>
  export let count;
</script>

<button on:click={() => count = count + 1}>Increment</button>
<p>{count}</p>
ASvelte throws a compile-time error preventing the assignment.
BThe component updates the count and re-renders with the new value.
CThe assignment is ignored silently; count remains unchanged.
DA runtime error occurs because props are immutable inside the child.
Attempts:
2 left
💡 Hint

Think about how Svelte treats props inside child components.

state_output
intermediate
2:00remaining
What is the output when a readonly prop is updated in the parent?

Given a parent component passing a prop to a child, what will the child display if the parent updates the prop value?

Svelte
<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
  let message = 'Hello';
  function update() {
    message = 'Hi';
  }
</script>
<button on:click={update}>Update Message</button>
<Child text={message} />

<!-- Child.svelte -->
<script>
  export let text;
</script>
<p>{text}</p>
AAlways shows 'Hello' because props are readonly and cannot update.
BShows 'Hi' immediately without clicking the button.
CInitially 'Hello', then changes to 'Hi' after clicking the button.
DThrows an error because the child tries to modify the prop.
Attempts:
2 left
💡 Hint

Consider how data flows from parent to child in Svelte.

📝 Syntax
advanced
2:00remaining
Which code correctly prevents a child from modifying a prop in Svelte?

Choose the code snippet that enforces the child component cannot modify the prop value.

A<script>export let value; // no reassignment allowed by Svelte</script>
B<script>export let value; Object.freeze(value);</script>
C<script>export let value; value = 10;</script>
D<script>export let value; value += 1;</script>
Attempts:
2 left
💡 Hint

Think about how Svelte treats props by default.

🔧 Debug
advanced
2:00remaining
Why does this Svelte child component throw an error?

Examine the child component code below. Why does it throw an error when the button is clicked?

Svelte
<script>
  export let score;
  function increase() {
    score += 1;
  }
</script>
<button on:click={increase}>Increase</button>
<p>{score}</p>
ABecause the button element is missing an aria-label.
BBecause props are readonly and cannot be reassigned inside the child.
CBecause score is not initialized with a default value.
DBecause the function increase is not bound to the button click event.
Attempts:
2 left
💡 Hint

Recall how Svelte treats props inside child components.

🧠 Conceptual
expert
3:00remaining
How can a Svelte child component communicate changes back to the parent when props are readonly?

Since props are readonly inside a Svelte child component, how can the child notify the parent to update a prop value?

AThe child dispatches a custom event that the parent listens to and updates the prop accordingly.
BThe child directly modifies the prop value using assignment.
CThe child uses a global variable to change the prop value.
DThe child imports the parent component and calls a method to update the prop.
Attempts:
2 left
💡 Hint

Think about how components communicate in Svelte without direct prop mutation.