0
0
Svelteframework~5 mins

Readonly props in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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>
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
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
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
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>
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.