What if your components could never accidentally break your app's data flow?
Why Readonly props in Svelte? - Purpose & Use Cases
Imagine you have a component that receives data from its parent, and you try to change that data inside the component directly.
For example, a child component tries to update a value it got from its parent, like changing a username or a setting.
Manually changing props inside a component can cause confusion and bugs because the parent still owns the original data.
This can lead to inconsistent states, unexpected behavior, and hard-to-find errors.
Readonly props make it clear that the data passed from parent to child should not be changed by the child.
This keeps data flow predictable and components easier to understand and maintain.
let name = propName; name = 'New Name'; // changes local copy but confuses data flowexport let name; // prop is readonly, child cannot reassign it
Readonly props enable clear, one-way data flow that keeps your app stable and easier to debug.
Think of a form component that shows user info passed from the parent. The form should not change the original user data directly but only send updates back through events.
Readonly props prevent accidental changes inside child components.
They keep data flow one-way and predictable.
This leads to fewer bugs and clearer code.
Practice
props are readonly inside a 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 CQuick Check:
Props are readonly = child cannot modify [OK]
- Thinking child can modify props directly
- Confusing readonly with immutable everywhere
- Believing parent cannot update props
Solution
Step 1: Recall Svelte prop syntax
Props are declared usingexport letto allow parent to pass values.Step 2: Check each option
Onlyexport let count;is valid syntax for a prop declaration.Final Answer:
export let count; -> Option DQuick Check:
Props declared with export let = A [OK]
- Using 'let export' instead of 'export let'
- Trying to use 'prop' keyword
- Using 'export const' which is not for props
<script>
export let name = 'Guest';
name = 'Alice';
</script>
<p>Hello {name}!</p>What will happen when this component runs?
Solution
Step 1: Identify the prop and reassignment
The propnameis declared with default 'Guest' but reassigned to 'Alice' inside the component.Step 2: Understand readonly prop behavior
Props are readonly inside the component, so reassigningnamecauses an error.Final Answer:
It will cause a runtime error because props are readonly. -> Option BQuick Check:
Reassigning prop inside component = error [OK]
- Assuming default prop value can be changed inside
- Thinking reassignment silently fails
- Confusing local variables with props
<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?
Solution
Step 1: Analyze prop reassignment in increment()
The function tries to increasecount, 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 AQuick Check:
Modifying prop inside component = error [OK]
- Thinking event handler fixes reassignment issue
- Assuming default prop value means local variable
- Ignoring readonly prop error
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 AQuick Check:
Update via event dispatch and local state = C [OK]
- Trying to reassign props directly in child
- Misusing export const for writable props
- Confusing bind:this with prop binding
Ready to prove it?
Pass the test and this pattern moves unknown
