What does it mean when a prop is readonly in Svelte?
A readonly prop is a property passed from a parent component that the child component cannot change. It is fixed and only the parent can update it.
Click to reveal answer
beginner
How do you declare a prop in Svelte that should not be changed inside the child component?
You declare the prop with export let in the child component and simply avoid changing it. Svelte does not allow direct reassignment of props inside the child.
Click to reveal answer
intermediate
Why is it important to treat props as readonly in Svelte components?
Treating props as readonly keeps data flow clear and predictable. It prevents bugs by ensuring only the parent controls the data, while the child just uses it.
Click to reveal answer
intermediate
What happens if you try to assign a new value to a prop inside a Svelte child component?
Svelte will give a compile-time warning or error because props are readonly inside the child. You should not reassign them directly.
Click to reveal answer
intermediate
How can a child component communicate changes back to the parent if props are readonly?
The child can send events to the parent using createEventDispatcher. The parent listens and updates the prop value, which flows back down.
Click to reveal answer
In Svelte, how do you receive a prop in a child component?
AUsing <code>export let propName</code>
BUsing <code>import propName</code>
CUsing <code>this.propName</code>
DUsing <code>props.propName</code>
✗ Incorrect
In Svelte, props are declared with export let to receive values from the parent.
What happens if you try to change a prop value inside a Svelte child component?
AIt updates the parent prop automatically
BSvelte throws a compile-time warning or error
CThe prop changes only inside the child
DNothing happens, it works fine
✗ Incorrect
Props are readonly in Svelte child components. Changing them causes a warning or error.
How can a child component notify the parent about a change if props are readonly?
ABy directly changing the prop
BBy using global variables
CBy using <code>createEventDispatcher</code> to send events
DBy importing the parent component
✗ Incorrect
The child uses createEventDispatcher to send events the parent listens to and updates props.
Why should props be treated as readonly in Svelte?
ABecause Svelte does not allow props at all
BTo allow child components to control parent data
CTo make components slower
DTo keep data flow clear and avoid bugs
✗ Incorrect
Readonly props keep data flow predictable and prevent bugs by avoiding unexpected changes.
Which of these is the correct way to declare a prop named 'title' in a Svelte component?
A<code>export let title;</code>
B<code>let title;</code>
C<code>const title = 'default';</code>
D<code>import title from 'parent';</code>
✗ Incorrect
Props are declared with export let to receive values from the parent component.
Explain how readonly props work in Svelte and why they are important.
Think about who owns the data and how changes should flow.
You got /4 concepts.
Describe how a child component can communicate a change back to the parent when props are readonly.
Focus on event communication between components.
You got /4 concepts.
Practice
(1/5)
1. In Svelte, what does it mean when we say props are readonly inside a child component?
easy
A. Props are only readonly if explicitly declared as such.
B. The parent component cannot send new values to the child.
C. The child component cannot change the value of props it receives from the parent.
D. Props can be changed freely inside the child component.
Solution
Step 1: Understand prop behavior in Svelte
Props passed from parent to child are fixed values inside the child and cannot be reassigned there.
Step 2: Identify what readonly means here
Readonly means the child cannot modify these props, ensuring data flows down safely.
Final Answer:
The child component cannot change the value of props it receives from the parent. -> Option C
Quick Check:
Props are readonly = child cannot modify [OK]
Hint: Props from parent are fixed inside child, no changes allowed [OK]
Common Mistakes:
Thinking child can modify props directly
Confusing readonly with immutable everywhere
Believing parent cannot update props
2. Which of the following is the correct way to declare a prop in a Svelte component?
easy
A. export const count;
B. let export count;
C. prop count;
D. export let count;
Solution
Step 1: Recall Svelte prop syntax
Props are declared using export let to allow parent to pass values.
Step 2: Check each option
Only export let count; is valid syntax for a prop declaration.
Final Answer:
export let count; -> Option D
Quick Check:
Props declared with export let = A [OK]
Hint: Props always start with 'export let' in Svelte [OK]
Common Mistakes:
Using 'let export' instead of 'export let'
Trying to use 'prop' keyword
Using 'export const' which is not for props
3. Given this Svelte component code:
<script>
export let name = 'Guest';
name = 'Alice';
</script>
<p>Hello {name}!</p>
What will happen when this component runs?
medium
A. It will display 'Hello Alice!' without errors.
B. It will cause a runtime error because props are readonly.
C. It will display 'Hello Guest!' and ignore the assignment.
D. It will display 'Hello undefined!' because of reassignment.
Solution
Step 1: Identify the prop and reassignment
The prop name is declared with default 'Guest' but reassigned to 'Alice' inside the component.
Step 2: Understand readonly prop behavior
Props are readonly inside the component, so reassigning name causes an error.
Final Answer:
It will cause a runtime error because props are readonly. -> Option B
Quick Check:
Reassigning prop inside component = error [OK]
Hint: Props cannot be reassigned inside component, causes error [OK]
Common Mistakes:
Assuming default prop value can be changed inside
Thinking reassignment silently fails
Confusing local variables with props
4. You have this Svelte component:
<script>
export let count = 0;
function increment() {
count += 1;
}
</script>
<button on:click={increment}>Increment</button>
<p>Count: {count}</p>
What is the problem here?
medium
A. Error because you cannot reassign a readonly prop inside the component.
B. No problem, count will increment on button click.
C. The button click event is not set up correctly.
D. The initial value of count must be passed from parent.
Solution
Step 1: Analyze prop reassignment in increment()
The function tries to increase count, which is a prop and readonly inside the component.
Step 2: Understand Svelte readonly prop rules
Reassigning a prop inside the component causes an error because props are readonly.
Final Answer:
Error because you cannot reassign a readonly prop inside the component. -> Option A
Quick Check:
Modifying prop inside component = error [OK]
Hint: Props cannot be changed inside component, use local state instead [OK]
Common Mistakes:
Thinking event handler fixes reassignment issue
Assuming default prop value means local variable
Ignoring readonly prop error
5. How can you allow a Svelte child component to update a value originally passed as a readonly prop from its parent?
hard
A. Use a local variable inside the child and dispatch an event to notify the parent of changes.
B. Directly reassign the prop inside the child component to update it.
C. Declare the prop with export const to make it writable.
D. Use bind:this on the prop to make it mutable.
Solution
Step 1: Recognize props are readonly in child
Props cannot be changed inside the child component directly.
Step 2: Use event dispatch to update parent
Child can keep a local copy and send events to parent to update the original value.
Final Answer:
Use a local variable inside the child and dispatch an event to notify the parent of changes. -> Option A
Quick Check:
Update via event dispatch and local state = C [OK]
Hint: Use local state + event dispatch to update parent from child [OK]
Common Mistakes:
Trying to reassign props directly in child
Misusing export const for writable props
Confusing bind:this with prop binding
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.