Skip to content

Commit f02b415

Browse files
committed
docs: clarify svelte:component migration to avoid lowercase component name gotcha
1 parent 17cb462 commit f02b415

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
@@ -612,6 +612,21 @@ To declare that a component of a certain type is required:
612612
613613
<svelte:component this={component} foo="bar" />
614614
```
615+
Or using the [new syntax](#svelte-component-no-longer-necessary):
616+
617+
```svelte
618+
<script lang="ts">
619+
import type { Component } from 'svelte';
620+
import {
621+
ComponentA,
622+
ComponentB
623+
} from 'component-library';
624+
let Component: Component<{ foo: string }> = $state(
625+
Math.random() ? ComponentA : ComponentB
626+
);
627+
</script>
628+
<Component foo="bar" />
629+
```
615630

616631
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 }>`).
617632

@@ -699,9 +714,9 @@ In Svelte 4, doing the following triggered reactivity:
699714

700715
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.
701716

702-
### `<svelte:component>` is no longer necessary
717+
### `<svelte:component>` is no longer necessary {#svelte-component-no-longer-necessary}
703718

704-
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>`.
719+
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>`.
705720

706721
This is no longer true in Svelte 5:
707722

@@ -722,6 +737,7 @@ This is no longer true in Svelte 5:
722737
<Thing />
723738
<svelte:component this={Thing} />
724739
```
740+
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.
725741

726742
### Touch and wheel events are passive
727743

0 commit comments

Comments
 (0)