0
0
Svelteframework~20 mins

Component composition in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Component Composer
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of nested components?

Consider two Svelte components: Parent.svelte and Child.svelte. The Parent component includes Child inside its markup. What will be the visible output when Parent is rendered?

Svelte
<!-- Child.svelte -->
<script>
  export let name = "Guest";
</script>
<p>Hello, {name}!</p>

<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
</script>
<div>
  <h1>Welcome</h1>
  <Child name="Alice" />
</div>
A<div><h1>Welcome</h1><p>Hello, Alice!</p></div>
B<div><h1>Welcome</h1></div>
C<div><h1>Welcome</h1><p>Hello, Guest!</p></div>
D<p>Hello, Alice!</p>
Attempts:
2 left
💡 Hint

Think about how props are passed from parent to child components in Svelte.

state_output
intermediate
2:00remaining
How does state update propagate in composed components?

Given a parent component that passes a writable store to a child component, what will be the displayed count after clicking the button once?

Svelte
<!-- Counter.svelte -->
<script>
  import { writable } from 'svelte/store';
  export let countStore;
</script>
<button on:click={() => countStore.update(n => n + 1)}>
  Increment
</button>
<p>Count: {$countStore}</p>

<!-- App.svelte -->
<script>
  import Counter from './Counter.svelte';
  import { writable } from 'svelte/store';
  const count = writable(0);
</script>
<Counter countStore={count} />
ACount: 1
BCount: 0
CCount: undefined
DCount: NaN
Attempts:
2 left
💡 Hint

Remember how Svelte stores work and how the $ prefix unwraps store values reactively.

📝 Syntax
advanced
2:00remaining
Which option correctly forwards all props to a child component?

In Svelte, you want a wrapper component to pass all received props to its child component without listing them individually. Which code snippet achieves this?

Svelte
<!-- Wrapper.svelte -->
<script>
  import Child from './Child.svelte';
</script>
<Child {...$$props} />
A<Child {...props} />
B<Child {...$props} />
C<Child {...this.props} />
D<Child {...$$props} />
Attempts:
2 left
💡 Hint

Check Svelte's special variable for forwarding all props.

🔧 Debug
advanced
2:00remaining
Why does this composed component fail to update on prop change?

Consider a parent component passing a prop to a child component. The child uses the prop to initialize a local variable but does not update when the prop changes. Why?

Svelte
<!-- Child.svelte -->
<script>
  export let count;
  let localCount = count;
</script>
<p>Local count: {localCount}</p>

<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
  let count = 0;
  function increment() {
    count += 1;
  }
</script>
<button on:click={increment}>Increment</button>
<Child count={count} />
AThe parent component does not pass the count prop properly.
BThe child component does not export the count prop correctly.
ClocalCount is only set once on component creation and does not react to prop changes.
DSvelte does not support passing props to child components.
Attempts:
2 left
💡 Hint

Think about how local variables initialized from props behave in Svelte.

🧠 Conceptual
expert
2:00remaining
What is the role of in Svelte component composition?

In Svelte, what does the <slot> element do inside a component?

A<slot> is a directive to conditionally render parts of the component.
B<slot> defines where child content passed between component tags will be inserted.
C<slot> automatically binds all props from parent to child components.
D<slot> is used to import external CSS styles into the component.
Attempts:
2 left
💡 Hint

Think about how you pass custom content inside component tags.