Skip to content

Commit fb8c7fa

Browse files
Update $props documentation to clarify when prop values are deeply reactive
1 parent e99e865 commit fb8c7fa

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

documentation/docs/02-runes/05-$props.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ Destructuring allows us to declare fallback values, which are used if the parent
4444
let { adjective = 'happy' } = $props();
4545
```
4646

47-
> [!NOTE] Fallback values are not turned into reactive state proxies.
48-
4947
## Renaming props
5048

5149
We can also use the destructuring assignment to rename props, which is necessary if they're invalid identifiers, or a JavaScript keyword like `super`:
@@ -94,6 +92,54 @@ References to a prop inside a component update when the prop itself updates —
9492
</button>
9593
```
9694

95+
!Prop variables not automatically deeply reactive. If a prop value is an object, setting a property of that object will not cause the component to update ([demo](/playground/untitled#H4sIAAAAAAAAE3VPS07DMBC9yshCaiuqBLYhjoQ4Q1eEReJOVIMztuJJBbJ8d-IkEqXQ5bx53yCo6VEU4kCs2eBR7EWnDXpRvAbBXy79EjDhK_PZucyf0XDC2sbjf7iyxEg82YjSq0E7rmqqWffODgwvJ22O0A22h02Wz9cq3TzVVOY_CioXrm3fUbEMQdmRuICHGCGvpiDGTxYFDyPG_Y3Cl_6_K199bpQ2yBDWBhBBwp0brPPb3Z-u7chsCSwpo9WHDNsdyApCMslzODUeyAJ23WSUsMUymyfBvYTHmmKcI2e9LyBcUmKKWyKulr_Fb2Z_SHPIAQAA)):
96+
97+
98+
<!-- prettier-ignore -->
99+
```svelte
100+
/// file: Child.svelte
101+
<script>
102+
let { object } = $props();
103+
</script>
104+
105+
<button onclick={() => {
106+
// has no effect
107+
object.count += 1
108+
}}>
109+
clicks: {object.count}
110+
</button>
111+
```
112+
113+
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=)):
114+
115+
116+
<!-- prettier-ignore -->
117+
```svelte
118+
/// file: App.svelte
119+
<script>
120+
import Child from './Child.svelte';
121+
122+
let object = $state({count: 0});
123+
</script>
124+
125+
<Child {object} />
126+
```
127+
128+
<!-- prettier-ignore -->
129+
```svelte
130+
/// file: Child.svelte
131+
<script>
132+
let { object } = $props();
133+
</script>
134+
135+
<button onclick={() => {
136+
// will cause the count below to update
137+
object.count += 1
138+
}}>
139+
clicks: {object.count}
140+
</button>
141+
```
142+
97143
## Type safety
98144

99145
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

Comments
 (0)