0
0
Svelteframework~20 mins

Dynamic route parameters in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Route Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output for this dynamic route?

Given a SvelteKit route file named src/routes/blog/[slug].svelte with the following code, what will be displayed when navigating to /blog/hello-world?

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

<h1>Blog Post: {params.slug}</h1>
A<h1>Blog Post: </h1>
B<h1>Blog Post: [slug]</h1>
C<h1>Blog Post: params.slug</h1>
D<h1>Blog Post: hello-world</h1>
Attempts:
2 left
💡 Hint

Dynamic route parameters are passed as params to the component.

📝 Syntax
intermediate
2:00remaining
Which code correctly accesses a dynamic parameter in a SvelteKit page?

Choose the option that correctly accesses the dynamic route parameter id in a file named src/routes/user/[id].svelte.

A<script>let userId = $page.params.id;</script>
B<script>export let params; let userId = params.id;</script>
C<script>import { page } from '$app/stores'; let userId = page.params.id;</script>
D<script>export let id; let userId = id;</script>
Attempts:
2 left
💡 Hint

Dynamic parameters are passed as params props to the page component.

state_output
advanced
2:00remaining
What is the value of currentId after navigation?

Consider this SvelteKit component in src/routes/product/[id].svelte:

<script>
  export let params;
  let currentId = params.id;
</script>

<h2>Product ID: {currentId}</h2>

If the user navigates from /product/123 to /product/456 without a full page reload, what will be the value of currentId?

A123
B456
Cundefined
DAn error is thrown
Attempts:
2 left
💡 Hint

Think about how export let params works with client-side navigation.

🔧 Debug
advanced
2:00remaining
Why does this dynamic route component show an empty slug?

Given this SvelteKit component src/routes/[slug].svelte:

<script>
  export let params;
</script>

<p>Slug: {params.slug}</p>

When navigating to /hello, the page shows Slug: (empty). What is the most likely cause?

AThe <code>params</code> prop is missing the <code>export let</code> declaration, so it is undefined
BThe <code>params</code> prop is not passed automatically; you must import it from <code>$page</code> store
CThe route parameter name in the filename does not match the accessed property; it should be <code>params.hello</code>
DThe route file is named incorrectly; it should be <code>[slug].js</code> instead of <code>[slug].svelte</code>
Attempts:
2 left
💡 Hint

Check how props are declared in Svelte components.

🧠 Conceptual
expert
3:00remaining
How does SvelteKit handle multiple dynamic parameters in nested routes?

Consider these two route files in SvelteKit:

  • src/routes/shop/[category]/[item].svelte
  • src/routes/shop/[category]/index.svelte

If a user navigates to /shop/electronics/phone, which parameters are available in [item].svelte and what are their values?

AOnly <code>params.item</code> is available with value <code>phone</code>; <code>params.category</code> is undefined
BOnly <code>params.category</code> is available with value <code>electronics</code>; <code>params.item</code> is undefined
CBoth <code>params.category</code> and <code>params.item</code> are available with values <code>electronics</code> and <code>phone</code> respectively
DNo <code>params</code> are available because nested dynamic routes do not pass parameters
Attempts:
2 left
💡 Hint

Think about how nested dynamic segments combine their parameters.