Skip to content

Commit 8c87b5c

Browse files
Rich-Harrisgithub-actions[bot]
authored andcommitted
sync svelte docs
1 parent 429af4e commit 8c87b5c

File tree

4 files changed

+164
-56
lines changed

4 files changed

+164
-56
lines changed

apps/svelte.dev/content/docs/svelte/02-runes/05-$props.md

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,29 @@ title: $props
55
The inputs to a component are referred to as _props_, which is short for _properties_. You pass props to components just like you pass attributes to elements:
66

77
```svelte
8+
<!--- file: App.svelte --->
89
<script>
910
import MyComponent from './MyComponent.svelte';
1011
</script>
1112
12-
/// file: App.svelte
1313
<MyComponent adjective="cool" />
1414
```
1515

1616
On the other side, inside `MyComponent.svelte`, we can receive props with the `$props` rune...
1717

1818
```svelte
19+
<!--- file: MyComponent.svelte --->
1920
<script>
2021
let props = $props();
2122
</script>
2223
23-
/// file: MyComponent.svelte
2424
<p>this component is {props.adjective}</p>
2525
```
2626

2727
...though more commonly, you'll [_destructure_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) your props:
2828

2929
```svelte
30-
/// file: MyComponent.svelte
30+
<!--- file: MyComponent.svelte --->
3131
<script>
3232
let +++{ adjective }+++ = $props();
3333
</script>
@@ -40,11 +40,10 @@ On the other side, inside `MyComponent.svelte`, we can receive props with the `$
4040
Destructuring allows us to declare fallback values, which are used if the parent component does not set a given prop:
4141

4242
```js
43-
/// file: MyComponent.svelte
4443
let { adjective = 'happy' } = $props();
4544
```
4645

47-
> [!NOTE] Fallback values are not turned into reactive state proxies.
46+
> [!NOTE] Fallback values are not turned into reactive state proxies (see [Updating props](#Updating-props) for more info)
4847
4948
## Renaming props
5049

@@ -66,9 +65,8 @@ let { a, b, c, ...others } = $props();
6665

6766
References to a prop inside a component update when the prop itself updates — when `count` changes in `App.svelte`, it will also change inside `Child.svelte`. But the child component is able to temporarily override the prop value, which can be useful for unsaved ephemeral state ([demo](/playground/untitled#H4sIAAAAAAAAE6WQ0WrDMAxFf0WIQR0Wmu3VTQJln7HsIfVcZubIxlbGRvC_DzuBraN92qPula50tODZWB1RPi_IX16jLALWSOOUq6P3-_ihLWftNEZ9TVeOWBNHlNhGFYznfqCBzeRdYHh6M_YVzsFNsNs3pdpGd4eBcqPVDMrNxNDBXeSRtXioDgO1zU8ataeZ2RE4Utao924RFXQ9iHXwvoPHKpW1xY4g_Bg0cSVhKS0p560Za95612ZC02ONrD8ZJYdZp_rGQ37ff_mSP86Np2TWZaNNmdcH56P4P67K66_SXoK9pG-5dF5Z9QEAAA==)):
6867

69-
<!-- prettier-ignore -->
7068
```svelte
71-
/// file: App.svelte
69+
<!--- file: App.svelte --->
7270
<script>
7371
import Child from './Child.svelte';
7472
@@ -82,9 +80,8 @@ References to a prop inside a component update when the prop itself updates —
8280
<Child {count} />
8381
```
8482

85-
<!-- prettier-ignore -->
8683
```svelte
87-
/// file: Child.svelte
84+
<!--- file: Child.svelte --->
8885
<script>
8986
let { count } = $props();
9087
</script>
@@ -94,6 +91,77 @@ References to a prop inside a component update when the prop itself updates —
9491
</button>
9592
```
9693

94+
Prop variables are not automatically deeply reactive. What happens when mutating one of their properties depends on what the parent passed in. For example if the parent passed a non-reactive POJO as a prop, setting a property of that object in the child will not cause the component to update ([demo](/playground/untitled#H4sIAAAAAAAAE3VPS07DMBC9yshCaiuqBLYhjoQ4Q1eEReJOVIMztuJJBbJ8d-IkEqXQ5bx53yCo6VEU4kCs2eBR7EWnDXpRvAbBXy79EjDhK_PZucyf0XDC2sbjf7iyxEg82YjSq0E7rmqqWffODgwvJ22O0A22h02Wz9cq3TzVVOY_CioXrm3fUbEMQdmRuICHGCGvpiDGTxYFDyPG_Y3Cl_6_K199bpQ2yBDWBhBBwp0brPPb3Z-u7chsCSwpo9WHDNsdyApCMslzODUeyAJ23WSUsMUymyfBvYTHmmKcI2e9LyBcUmKKWyKulr_Fb2Z_SHPIAQAA)):
95+
96+
```svelte
97+
<!--- file: App.svelte --->
98+
<script>
99+
import Child from './Child.svelte';
100+
101+
let object = $state({count: 0});
102+
</script>
103+
104+
<Child {object} />
105+
```
106+
107+
```svelte
108+
<!--- file: Child.svelte --->
109+
<script>
110+
let { object } = $props();
111+
</script>
112+
113+
<button onclick={() => {
114+
// has no effect
115+
object.count += 1
116+
}}>
117+
clicks: {object.count}
118+
</button>
119+
```
120+
121+
However if the value passed in by the parent component is itself a deeply reactive state object, then it will be deeply reactive in the child, too ([demo](/playground/untitled#H4sIAAAAAAAAE3WQwU7DMBBEf2VlITUVVQLXkERC_YaeCIfE2aoujm3FGwqy_O_YcSug0KNnx7Nv1jHVjchKtlMkSOLANmwvJFpWvjhGnybOohD0s_PZmNy-o6So9Z3F_3SuFaGiEMMqyydhqGlVS2I0eiLYHoQcYD_pEVZ5sbzOX1dPwRaMEgl0f0ROUMOdpY4wc1zPikp48OvgqorvXFWlRJe-eCiawED4QaykaUa_udHl5-rfba4mN_pETHcB9RHVTNrY7C9gPxNpBVpxKfhb7bI11A24GFIUcBJSAu9mi0AHhKUo9Cj1CUjDbIbQP1rTpjzN72t4bJX3C8kSa8vLCZLFR4q0-eogr_4LN7sC9foBAAA=)):
122+
123+
```svelte
124+
<!--- file: App.svelte --->
125+
<script>
126+
import Child from './Child.svelte';
127+
128+
let object = $state({count: 0});
129+
</script>
130+
131+
<Child {object} />
132+
```
133+
134+
```svelte
135+
<!--- file: Child.svelte --->
136+
<script>
137+
let { object } = $props();
138+
</script>
139+
140+
<button onclick={() => {
141+
// will cause the count below to update
142+
object.count += 1
143+
}}>
144+
clicks: {object.count}
145+
</button>
146+
```
147+
148+
The fallback value of a prop not declared with `$bindable` is treated like a non-reactive POJO, and therefore also doesn't update the component when mutating its properties.
149+
150+
```svelte
151+
<!--- file: Child.svelte --->
152+
<script>
153+
let { object = { count = 0 } } = $props();
154+
</script>
155+
<button onclick={() => {
156+
// has no effect if the fallback value is used
157+
object.count += 1
158+
}}>
159+
clicks: {object.count}
160+
</button>
161+
```
162+
163+
In general, mutating props is discouraged, instead use callback props to make it easier to reason about state and changes to that state. If parent and child should share (and be allowed to mutate) the same object, then use the [$bindable]($bindable) rune.
164+
97165
## Type safety
98166

99167
You can add type safety to your components by annotating your props, as you would with any other variable declaration. In TypeScript that might look like this...

apps/svelte.dev/content/docs/svelte/03-template-syntax/05-await.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,12 @@ Similarly, if you only want to show the error state, you can omit the `then` blo
6868
<p>The error is {error}</p>
6969
{/await}
7070
```
71+
72+
> [!NOTE] You can use `#await` with [`import(...)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) to render components lazily:
73+
>
74+
> ```svelte
75+
> {#await import('./Component.svelte') then { default: Component }}
76+
> <Component />
77+
> {/await}
78+
> ```
79+

apps/svelte.dev/content/docs/svelte/07-misc/07-v5-migration-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ If you wanted multiple UI placeholders, you had to use named slots. In Svelte 5,
376376
</main>
377377
378378
<footer>
379-
---<slot name="header" />---
379+
---<slot name="footer" />---
380380
+++{@render footer()}+++
381381
</footer>
382382
```

apps/svelte.dev/content/docs/svelte/98-reference/30-runtime-warnings.md

Lines changed: 77 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,131 +2,162 @@
22
title: 'Runtime warnings'
33
---
44

5-
## Client warnings
5+
## Client errors
66

7-
### binding_property_non_reactive
7+
### bind_invalid_checkbox_value
88

99
```
10-
`%binding%` is binding to a non-reactive property
10+
Using `bind:value` together with a checkbox input is not allowed. Use `bind:checked` instead
1111
```
1212

13+
### bind_invalid_export
14+
15+
```
16+
Component %component% has an export named `%key%` that a consumer component is trying to access using `bind:%key%`, which is disallowed. Instead, use `bind:this` (e.g. `<%name% bind:this={component} />`) and then access the property on the bound component instance (e.g. `component.%key%`)
17+
```
18+
19+
### bind_not_bindable
20+
1321
```
14-
`%binding%` (%location%) is binding to a non-reactive property
22+
A component is attempting to bind to a non-bindable property `%key%` belonging to %component% (i.e. `<%name% bind:%key%={...}>`). To mark a property as bindable: `let { %key% = $bindable() } = $props()`
1523
```
1624

17-
### console_log_state
25+
### component_api_changed
1826

1927
```
20-
Your `console.%method%` contained `$state` proxies. Consider using `$inspect(...)` or `$state.snapshot(...)` instead
28+
%parent% called `%method%` on an instance of %component%, which is no longer valid in Svelte 5. See https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes for more information
2129
```
2230

23-
When logging a [proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy), browser devtools will log the proxy itself rather than the value it represents. In the case of Svelte, the 'target' of a `$state` proxy might not resemble its current value, which can be confusing.
31+
### component_api_invalid_new
2432

25-
The easiest way to log a value as it changes over time is to use the [`$inspect`](https://svelte.dev/docs/svelte/$inspect) rune. Alternatively, to log things on a one-off basis (for example, inside an event handler) you can use [`$state.snapshot`](https://svelte.dev/docs/svelte/$state#$state.snapshot) to take a snapshot of the current value.
33+
```
34+
Attempted to instantiate %component% with `new %name%`, which is no longer valid in Svelte 5. If this component is not under your control, set the `compatibility.componentApi` compiler option to `4` to keep it working. See https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes for more information
35+
```
2636

27-
### event_handler_invalid
37+
### derived_references_self
2838

2939
```
30-
%handler% should be a function. Did you mean to %suggestion%?
40+
A derived value cannot reference itself recursively
3141
```
3242

33-
### hydration_attribute_changed
43+
### each_key_duplicate
44+
45+
```
46+
Keyed each block has duplicate key at indexes %a% and %b%
47+
```
3448

3549
```
36-
The `%attribute%` attribute on `%html%` changed its value between server and client renders. The client value, `%value%`, will be ignored in favour of the server value
50+
Keyed each block has duplicate key `%value%` at indexes %a% and %b%
3751
```
3852

39-
### hydration_html_changed
53+
### effect_in_teardown
4054

4155
```
42-
The value of an `{@html ...}` block changed between server and client renders. The client value will be ignored in favour of the server value
56+
`%rune%` cannot be used inside an effect cleanup function
4357
```
4458

59+
### effect_in_unowned_derived
60+
4561
```
46-
The value of an `{@html ...}` block %location% changed between server and client renders. The client value will be ignored in favour of the server value
62+
Effect cannot be created inside a `$derived` value that was not itself created inside an effect
4763
```
4864

49-
### hydration_mismatch
65+
### effect_orphan
5066

5167
```
52-
Hydration failed because the initial UI does not match what was rendered on the server
68+
`%rune%` can only be used inside an effect (e.g. during component initialisation)
5369
```
5470

71+
### effect_update_depth_exceeded
72+
5573
```
56-
Hydration failed because the initial UI does not match what was rendered on the server. The error occurred near %location%
74+
Maximum update depth exceeded. This can happen when a reactive block or effect repeatedly sets a new value. Svelte limits the number of nested updates to prevent infinite loops
5775
```
5876

59-
### invalid_raw_snippet_render
77+
### hydration_failed
6078

6179
```
62-
The `render` function passed to `createRawSnippet` should return HTML for a single element
80+
Failed to hydrate the application
6381
```
6482

65-
### legacy_recursive_reactive_block
83+
### invalid_snippet
6684

6785
```
68-
Detected a migrated `$:` reactive block in `%filename%` that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.
86+
Could not `{@render}` snippet due to the expression being `null` or `undefined`. Consider using optional chaining `{@render snippet?.()}`
6987
```
7088

71-
### lifecycle_double_unmount
89+
### lifecycle_legacy_only
7290

7391
```
74-
Tried to unmount a component that was not mounted
92+
`%name%(...)` cannot be used in runes mode
7593
```
7694

77-
### ownership_invalid_binding
95+
### props_invalid_value
7896

7997
```
80-
%parent% passed a value to %child% with `bind:`, but the value is owned by %owner%. Consider creating a binding between %owner% and %parent%
98+
Cannot do `bind:%key%={undefined}` when `%key%` has a fallback value
8199
```
82100

83-
### ownership_invalid_mutation
101+
### props_rest_readonly
84102

85103
```
86-
Mutating a value outside the component that created it is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead
104+
Rest element properties of `$props()` such as `%property%` are readonly
87105
```
88106

107+
### rune_outside_svelte
108+
89109
```
90-
%component% mutated a value owned by %owner%. This is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead
110+
The `%rune%` rune is only available inside `.svelte` and `.svelte.js/ts` files
91111
```
92112

93-
### state_proxy_equality_mismatch
113+
### state_descriptors_fixed
94114

95115
```
96-
Reactive `$state(...)` proxies and the values they proxy have different identities. Because of this, comparisons with `%operator%` will produce unexpected results
116+
Property descriptors defined on `$state` objects must contain `value` and always be `enumerable`, `configurable` and `writable`.
97117
```
98118

99-
`$state(...)` creates a [proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) of the value it is passed. The proxy and the value have different identities, meaning equality checks will always return `false`:
119+
### state_prototype_fixed
120+
121+
```
122+
Cannot set prototype of `$state` object
123+
```
100124

101-
```svelte
102-
<script>
103-
let value = { foo: 'bar' };
104-
let proxy = $state(value);
125+
### state_unsafe_local_read
105126

106-
value === proxy; // always false
107-
</script>
108127
```
128+
Reading state that was created inside the same derived is forbidden. Consider using `untrack` to read locally created state
129+
```
130+
131+
### state_unsafe_mutation
109132

110-
To resolve this, ensure you're comparing values where both values were created with `$state(...)`, or neither were. Note that `$state.raw(...)` will _not_ create a state proxy.
133+
```
134+
Updating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without `$state`
135+
```
111136

112137

113-
## Shared warnings
138+
## Shared errors
114139

115-
### dynamic_void_element_content
140+
### invalid_default_snippet
116141

117142
```
118-
`<svelte:element this="%tag%">` is a void element — it cannot have content
143+
Cannot use `{@render children(...)}` if the parent component uses `let:` directives. Consider using a named snippet instead
119144
```
120145

121-
### state_snapshot_uncloneable
146+
### lifecycle_outside_component
122147

123148
```
124-
Value cannot be cloned with `$state.snapshot` — the original value was returned
149+
`%name%(...)` can only be used during component initialisation
125150
```
126151

152+
### store_invalid_shape
153+
127154
```
128-
The following properties cannot be cloned with `$state.snapshot` — the return value contains the originals:
155+
`%name%` is not a store with a `subscribe` method
156+
```
157+
158+
### svelte_element_invalid_this_value
129159

130-
%properties%
160+
```
161+
The `this` prop on `<svelte:element>` must be a string, if defined
131162
```
132163

0 commit comments

Comments
 (0)