Question · Svelte and SvelteKit course · Module 2: Components

Dynamic Component

How do you render a component stored in a variable in Svelte 5?

Answer options

  • Use the variable as a tag, e.g. <Selected />, where Selected = $derived(components[selected])
  • Write <{Component} />, where Component is the variable holding the component
  • Use the <dynamic component={Component} /> tag
  • Put ${Component} where the tag should be
Show the answer

Correct answer:

  • Use the variable as a tag, e.g. <Selected />, where Selected = $derived(components[selected])