Skip to content

Commit b9c86c0

Browse files
committed
3.5: props destructure extras
1 parent 83a0cf1 commit b9c86c0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/guide/components/props.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,34 @@ In addition, you can use JavaScript's native default value syntax to declare def
152152
const { foo = 'hello' } = defineProps<{ foo?: string }>()
153153
```
154154

155+
If you prefer to have more visual distinction between destructured props and normal variables in your IDE, Vue's VSCode extension provides a setting to enable inlay-hints for destructured props.
156+
155157
### Passing Destructured Props into Functions
156158

159+
When we pass a destructured prop into a function, e.g.:
160+
161+
```js
162+
const { foo } = defineProps(['foo'])
163+
164+
watch(foo, /* ... */)
165+
```
166+
167+
This will not work as expected because it is equivalent to `watch(props.foo, ...)` - we are passing a value instead of a reactive data source to `watch`. In fact, Vue's compiler will catch such cases and throw a warning.
168+
169+
Similar to how we can watch a normal prop with `watch(() => props.foo, ...)`, we can watch a destructured prop also by wrapping it in a getter:
170+
171+
```js
172+
watch(() => foo, /* ... */)
173+
```
174+
175+
In addition, this is the recommended approach when we need to pass a destructured prop into an external function while retaining reactivity:
176+
177+
```js
178+
useComposable(() => foo)
179+
```
180+
181+
The external function can call the getter (or normalize it with [toValue](/api/reactivity-utilities.html#tovalue)) when it needs to track changes of the provided prop, e.g. in a computed or watcher getter.
182+
157183
</div>
158184

159185
## Prop Passing Details {#prop-passing-details}

0 commit comments

Comments
 (0)