Skip to content

Commit 213d70b

Browse files
committed
docs: clarify svelte:component migration to avoid lowercase component name gotcha
1 parent 551284c commit 213d70b

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

documentation/docs/07-misc/07-v5-migration-guide.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,21 @@ To declare that a component of a certain type is required:
613613
614614
<svelte:component this={component} foo="bar" />
615615
```
616+
Or using the [new syntax](#svelte-component-no-longer-necessary):
617+
618+
```svelte
619+
<script lang="ts">
620+
import type { Component } from 'svelte';
621+
import {
622+
ComponentA,
623+
ComponentB
624+
} from 'component-library';
625+
let Component: Component<{ foo: string }> = $state(
626+
Math.random() ? ComponentA : ComponentB
627+
);
628+
</script>
629+
<Component foo="bar" />
630+
```
616631

617632
The two utility types `ComponentEvents` and `ComponentType` are also deprecated. `ComponentEvents` is obsolete because events are defined as callback props now, and `ComponentType` is obsolete because the new `Component` type is the component type already (e.g. `ComponentType<SvelteComponent<{ prop: string }>>` == `Component<{ prop: string }>`).
618633

@@ -700,9 +715,9 @@ In Svelte 4, doing the following triggered reactivity:
700715

701716
This is because the Svelte compiler treated the assignment to `foo.value` as an instruction to update anything that referenced `foo`. In Svelte 5, reactivity is determined at runtime rather than compile time, so you should define `value` as a reactive `$state` field on the `Foo` class. Wrapping `new Foo()` with `$state(...)` will have no effect — only vanilla objects and arrays are made deeply reactive.
702717

703-
### `<svelte:component>` is no longer necessary
718+
### `<svelte:component>` is no longer necessary {#svelte-component-no-longer-necessary}
704719

705-
In Svelte 4, components are _static_ — if you render `<Thing>`, and the value of `Thing` changes, [nothing happens](/playground/7f1fa24f0ab44c1089dcbb03568f8dfa?version=4.2.18). To make it dynamic you must use `<svelte:component>`.
720+
In Svelte 4, components are _static_ — if you render `<Thing>`, and the value of `Thing` changes, [nothing happens](/playground/7f1fa24f0ab44c1089dcbb03568f8dfa?version=4.2.18). To make it dynamic you had to use `<svelte:component>`.
706721

707722
This is no longer true in Svelte 5:
708723

@@ -723,6 +738,7 @@ This is no longer true in Svelte 5:
723738
<Thing />
724739
<svelte:component this={Thing} />
725740
```
741+
While migrating, keep in mind that your user-defined component's name should be capitalized (`Thing`) to distinguish it from a normal HTML element and avoid incorrect types.
726742

727743
### Touch and wheel events are passive
728744

0 commit comments

Comments
 (0)