Skip to content

Commit

Permalink
3.5: props destructure extras
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 2, 2024
1 parent 1564445 commit a06ac21
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/guide/components/props.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,34 @@ In addition, you can use JavaScript's native default value syntax to declare def
const { foo = 'hello' } = defineProps<{ foo?: string }>()
```

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.

### Passing Destructured Props into Functions

When we pass a destructured prop into a function, e.g.:

```js
const { foo } = defineProps(['foo'])

watch(foo, /* ... */)
```

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.

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:

```js
watch(() => foo, /* ... */)
```

In addition, this is the recommended approach when we need to pass a destructured prop into an external function while retaining reactivity:

```js
useComposable(() => foo)
```

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.

</div>

## Prop Passing Details {#prop-passing-details}
Expand Down

0 comments on commit a06ac21

Please sign in to comment.