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: apps/svelte.dev/content/docs/svelte/02-runes/05-$props.md
+77-9Lines changed: 77 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -5,29 +5,29 @@ title: $props
5
5
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:
6
6
7
7
```svelte
8
+
<!--- file: App.svelte --->
8
9
<script>
9
10
import MyComponent from './MyComponent.svelte';
10
11
</script>
11
12
12
-
/// file: App.svelte
13
13
<MyComponent adjective="cool" />
14
14
```
15
15
16
16
On the other side, inside `MyComponent.svelte`, we can receive props with the `$props` rune...
17
17
18
18
```svelte
19
+
<!--- file: MyComponent.svelte --->
19
20
<script>
20
21
let props = $props();
21
22
</script>
22
23
23
-
/// file: MyComponent.svelte
24
24
<p>this component is {props.adjective}</p>
25
25
```
26
26
27
27
...though more commonly, you'll [_destructure_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) your props:
28
28
29
29
```svelte
30
-
/// file: MyComponent.svelte
30
+
<!--- file: MyComponent.svelte --->
31
31
<script>
32
32
let +++{ adjective }+++ = $props();
33
33
</script>
@@ -40,11 +40,10 @@ On the other side, inside `MyComponent.svelte`, we can receive props with the `$
40
40
Destructuring allows us to declare fallback values, which are used if the parent component does not set a given prop:
41
41
42
42
```js
43
-
/// file: MyComponent.svelte
44
43
let { adjective ='happy' } =$props();
45
44
```
46
45
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)
48
47
49
48
## Renaming props
50
49
@@ -66,9 +65,8 @@ let { a, b, c, ...others } = $props();
66
65
67
66
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==)):
68
67
69
-
<!-- prettier-ignore -->
70
68
```svelte
71
-
/// file: App.svelte
69
+
<!--- file: App.svelte --->
72
70
<script>
73
71
import Child from './Child.svelte';
74
72
@@ -82,9 +80,8 @@ References to a prop inside a component update when the prop itself updates —
82
80
<Child {count} />
83
81
```
84
82
85
-
<!-- prettier-ignore -->
86
83
```svelte
87
-
/// file: Child.svelte
84
+
<!--- file: Child.svelte --->
88
85
<script>
89
86
let { count } = $props();
90
87
</script>
@@ -94,6 +91,77 @@ References to a prop inside a component update when the prop itself updates —
94
91
</button>
95
92
```
96
93
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
+
97
165
## Type safety
98
166
99
167
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...
0 commit comments