You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/docs/03-template-syntax/11-bind.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,10 +12,34 @@ The general syntax is `bind:property={expression}`, where `expression` is an _lv
12
12
<input bind:value />
13
13
```
14
14
15
+
15
16
Svelte creates an event listener that updates the bound value. If an element already has a listener for the same event, that listener will be fired before the bound value is updated.
16
17
17
18
Most bindings are _two-way_, meaning that changes to the value will affect the element and vice versa. A few bindings are _readonly_, meaning that changing their value will have no effect on the element.
18
19
20
+
## Function bindings
21
+
22
+
You can also use `bind:property={get, set}`, where `get` and `set` are functions, allowing you to perform validation and transformation:
23
+
24
+
```svelte
25
+
<input bind:value={
26
+
() => value,
27
+
(v) => value = v.toLowerCase()}
28
+
/>
29
+
```
30
+
31
+
In the case of readonly bindings like [dimension bindings](#Dimensions), the `get` value should be `null`:
32
+
33
+
```svelte
34
+
<div
35
+
bind:clientWidth={null, redraw}
36
+
bind:clientHeight={null, redraw}
37
+
>...</div>
38
+
```
39
+
40
+
> [!NOTE]
41
+
> Function bindings are available in Svelte 5.9.0 and newer.
42
+
19
43
## `<input bind:value>`
20
44
21
45
A `bind:value` directive on an `<input>` element binds the input's `value` property:
Copy file name to clipboardExpand all lines: documentation/docs/98-reference/.generated/client-warnings.md
+75-31Lines changed: 75 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -66,6 +66,31 @@ The easiest way to log a value as it changes over time is to use the [`$inspect`
66
66
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
67
67
```
68
68
69
+
Certain attributes like `src` on an `<img>` element will not be repaired during hydration, i.e. the server value will be kept. That's because updating these attributes can cause the image to be refetched (orinthecaseofan`<iframe>`, fortheframetobereloaded), even if they resolve to the same resource.
70
+
71
+
To fix this, either silence the warning with a [`svelte-ignore`](basic-markup#Comments) comment, or ensure that the value stays the same between server and client. If you really need the value to change on hydration, you can force an update like this:
72
+
73
+
```svelte
74
+
<script>
75
+
let { src } =$props();
76
+
77
+
if (typeofwindow!=='undefined') {
78
+
// stash the value...
79
+
constinitial= src;
80
+
81
+
// unset it...
82
+
src =undefined;
83
+
84
+
$effect(() => {
85
+
// ...and reset after we've mounted
86
+
src = initial;
87
+
});
88
+
}
89
+
</script>
90
+
91
+
<img {src} />
92
+
```
93
+
69
94
### hydration_html_changed
70
95
71
96
```
@@ -76,6 +101,31 @@ The value of an `{@html ...}` block changed between server and client renders. T
76
101
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
77
102
```
78
103
104
+
If the `{@html ...}` value changes between the server and the client, it will not be repaired during hydration, i.e. the server value will be kept. That's because change detection during hydration is expensive and usually unnecessary.
105
+
106
+
To fix this, either silence the warning with a [`svelte-ignore`](basic-markup#Comments) comment, or ensure that the value stays the same between server and client. If you really need the value to change on hydration, you can force an update like this:
107
+
108
+
```svelte
109
+
<script>
110
+
let { markup } =$props();
111
+
112
+
if (typeofwindow!=='undefined') {
113
+
// stash the value...
114
+
constinitial= markup;
115
+
116
+
// unset it...
117
+
markup =undefined;
118
+
119
+
$effect(() => {
120
+
// ...and reset after we've mounted
121
+
markup = initial;
122
+
});
123
+
}
124
+
</script>
125
+
126
+
{@html markup}
127
+
```
128
+
79
129
### hydration_mismatch
80
130
81
131
```
@@ -86,6 +136,10 @@ Hydration failed because the initial UI does not match what was rendered on the
86
136
Hydration failed because the initial UI does not match what was rendered on the server. The error occurred near %location%
87
137
```
88
138
139
+
This warning is thrown when Svelte encounters an error while hydrating the HTML from the server. During hydration, Svelte walks the DOM, expecting a certain structure. If that structure is different (for example because the HTML was repaired by the DOM because of invalid HTML), then Svelte will run into issues, resulting in this warning.
140
+
141
+
During development, this error is often preceeded by a `console.error` detailing the offending HTML, which needs fixing.
142
+
89
143
### invalid_raw_snippet_render
90
144
91
145
```
@@ -110,6 +164,10 @@ Tried to unmount a component that was not mounted
110
164
%parent% passed a value to %child% with `bind:`, but the value is owned by %owner%. Consider creating a binding between %owner% and %parent%
111
165
```
112
166
167
+
Consider three components `GrandParent`, `Parent` and `Child`. If you do `<GrandParent bind:value>`, inside `GrandParent` pass on the variable via `<Parent {value} />` (notethemissing`bind:`) and then do `<Child bind:value>` inside `Parent`, this warning is thrown.
168
+
169
+
To fix it, `bind:` to the value instead of just passing a property (i.e. in this example do `<Parent bind:value />`).
170
+
113
171
### ownership_invalid_mutation
114
172
115
173
```
@@ -120,45 +178,31 @@ Mutating a value outside the component that created it is strongly discouraged.
120
178
%component% mutated a value owned by %owner%. This is strongly discouraged. Consider passing values to child components with `bind:`, or use a callback instead
121
179
```
122
180
123
-
### reactive_declaration_non_reactive_property
124
-
125
-
```
126
-
A `$:` statement (%location%) read reactive state that was not visible to the compiler. Updates to this state will not cause the statement to re-run. The behaviour of this code will change if you migrate it to runes mode
127
-
```
128
-
129
-
In legacy mode, a `$:` [reactive statement](https://svelte.dev/docs/svelte/legacy-reactive-assignments) re-runs when the state it _references_ changes. This is determined at compile time, by analysing the code.
130
-
131
-
In runes mode, effects and deriveds re-run when there are changes to the values that are read during the function's _execution_.
181
+
Consider the following code:
132
182
133
-
Often, the result is the same — for example these can be considered equivalent:
134
-
135
-
```js
136
-
let a = 1, b = 2, sum = 3;
137
-
// ---cut---
138
-
$: sum = a + b;
139
-
```
183
+
```svelte
184
+
<!--- file: App.svelte --->
185
+
<script>
186
+
import Child from './Child.svelte';
187
+
let person = $state({ name:'Florida', surname:'Man' });
188
+
</script>
140
189
141
-
```js
142
-
let a = 1, b = 2;
143
-
// ---cut---
144
-
const sum = $derived(a + b);
190
+
<Child {person} />
145
191
```
146
192
147
-
In some cases — such as the one that triggered the above warning — they are _not_ the same:
148
-
149
-
```js
150
-
let a = 1, b = 2, sum = 3;
151
-
// ---cut---
152
-
const add = () => a + b;
193
+
```svelte
194
+
<!--- file:Child.svelte--->
195
+
<script>
196
+
let { person } =$props();
197
+
</script>
153
198
154
-
// the compiler can't 'see' that `sum` depends on `a` and `b`, but
155
-
// they _would_ be read while executing the `$derived` version
156
-
$: sum = add();
199
+
<input bind:value={person.name}>
200
+
<input bind:value={person.surname}>
157
201
```
158
202
159
-
Similarly, reactive properties of [deep state](https://svelte.dev/docs/svelte/$state#Deep-state) are not visible to the compiler. As such, changes to these properties will cause effects and deriveds to re-run but will _not_ cause `$:` statements to re-run.
203
+
`Child` is mutating `person` which is owned by `App` without being explicitly "allowed" to do so. This is strongly discouraged since it can create code that is hard to reason about at scale ("who mutated this value?"), hence the warning.
160
204
161
-
When you [migrate this component](https://svelte.dev/docs/svelte/v5-migration-guide) to runes mode, the behaviour will change accordingly.
205
+
To fix it, either create callback props to communicate changes, or mark `person` as [`$bindable`]($bindable).
0 commit comments