8000 docs: clarify the usage of toRefs and toRef for optional props (#639) · vuejs/docs@a502ebe · GitHub
[go: up one dir, main page]

Skip to content

Commit a502ebe

Browse files
docs: clarify the usage of toRefs and toRef for optional props (#639)
1 parent ec79709 commit a502ebe

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/api/refs-api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ export default {
8484
}
8585
```
8686

87+
`toRef` will return a usable ref even if the source property doesn't currently exist. This makes it especially useful when working with optional props, which wouldn't be picked up by [`toRefs`](#torefs).
88+
8789
## `toRefs`
8890

8991
Converts a reactive object to a plain object where each property of the resulting object is a [`ref`](#ref) pointing to the corresponding property of the original object.
@@ -140,6 +142,8 @@ export default {
140142
}
141143
```
142144

145+
`toRefs` will only generate refs for properties that are included in the source object. To create a ref for a specific property use [`toRef`](#toref) instead.
146+
143147
## `isRef`
144148

145149
Checks if a value is a ref object.

src/guide/composition-api-introduction.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ Let’s imagine that in our app, we have a view to show a list of repositories o
1818
export default {
1919
components: { RepositoriesFilters, RepositoriesSortBy, RepositoriesList },
2020
props: {
21-
user: { type: String }
21+
user: {
22+
type: String,
23+
required: true
24+
}
2225
},
2326
data () {
2427
return {
@@ -86,7 +89,10 @@ Let’s add `setup` to our component:
8689
export default {
8790
components: { RepositoriesFilters, RepositoriesSortBy, RepositoriesList },
8891
props: {
89-
user: { type: String }
92+
user: {
93+
type: String,
94+
required: true
95+
}
9096
},
9197
setup(props) {
9298
console.log(props) // { user: '' }
@@ -192,7 +198,10 @@ import { ref } from 'vue'
192198
export default {
193199
components: { RepositoriesFilters, RepositoriesSortBy, RepositoriesList },
194200
props: {
195-
user: { type: String }
201+
user: {
202+
type: String,
203+
required: true
204+
}
196205
},
197206
setup (props) {
198207
const repositories = ref([])
@@ -449,7 +458,10 @@ import { toRefs } from 'vue'
449458
export default {
450459
components: { RepositoriesFilters, RepositoriesSortBy, RepositoriesList },
451460
props: {
452-
user: { type: String }
461+
user: {
462+
type: String,
463+
required: true
464+
}
453465
},
454466
setup (props) {
455467
const { user } = toRefs(props)
@@ -495,7 +507,10 @@ import useRepositoryFilters from '@/composables/useRepositoryFilters'
495507
export default {
496508
components: { RepositoriesFilters, RepositoriesSortBy, RepositoriesList },
497509
props: {
498-
user: { type: String }
510+
user: {
511+
type: String,
512+
required: true
513+
}
499514
},
500515
setup(props) {
501516
const { user } = toRefs(props)

src/guide/composition-api-setup.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default {
3434
However, because `props` are reactive, you **cannot use ES6 destructuring** because it will remove props reactivity.
3535
:::
3636

37-
If you need to destructure your props, you can do this safely by utilizing the [toRefs](reactivity-fundamentals.html#destructuring-reactive-state) inside of the `setup` function.
37+
If you need to destructure your props, you can do this by utilizing the [toRefs](reactivity-fundamentals.html#destructuring-reactive-state) inside of the `setup` function:
3838

3939
```js
4040
// MyBook.vue
@@ -48,6 +48,20 @@ setup(props) {
4848
}
4949
```
5050

51+
If `title` is an optional prop, it could be missing from `props`. In that case, `toRefs` won't create a ref for `title`. Instead you'd need to use `toRef`:
52+
53+
```js
54+
// MyBook.vue
55+
56+
import { toRef } from 'vue'
57+
58+
setup(props) {
59+
const title = toRef(props, 'title')
60+
61+
console.log(title.value)
62+
}
63+
```
64+
5165
### Context
5266

5367
The second argument passed to the `setup` function is the `context`. The `context` is a normal JavaScript object that exposes three component properties:

0 commit comments

Comments
 (0)
0