diff --git a/src/api/refs-api.md b/src/api/refs-api.md index 46d26f49b6..766e7550fd 100644 --- a/src/api/refs-api.md +++ b/src/api/refs-api.md @@ -84,6 +84,8 @@ export default { } ``` +`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). + ## `toRefs` 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 { } ``` +`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. + ## `isRef` Checks if a value is a ref object. diff --git a/src/guide/composition-api-introduction.md b/src/guide/composition-api-introduction.md index ffae321d2f..da2e990b4c 100644 --- a/src/guide/composition-api-introduction.md +++ b/src/guide/composition-api-introduction.md @@ -18,7 +18,10 @@ Let’s imagine that in our app, we have a view to show a list of repositories o export default { components: { RepositoriesFilters, RepositoriesSortBy, RepositoriesList }, props: { - user: { type: String } + user: { + type: String, + required: true + } }, data () { return { @@ -86,7 +89,10 @@ Let’s add `setup` to our component: export default { components: { RepositoriesFilters, RepositoriesSortBy, RepositoriesList }, props: { - user: { type: String } + user: { + type: String, + required: true + } }, setup(props) { console.log(props) // { user: '' } @@ -192,7 +198,10 @@ import { ref } from 'vue' export default { components: { RepositoriesFilters, RepositoriesSortBy, RepositoriesList }, props: { - user: { type: String } + user: { + type: String, + required: true + } }, setup (props) { const repositories = ref([]) @@ -449,7 +458,10 @@ import { toRefs } from 'vue' export default { components: { RepositoriesFilters, RepositoriesSortBy, RepositoriesList }, props: { - user: { type: String } + user: { + type: String, + required: true + } }, setup (props) { const { user } = toRefs(props) @@ -495,7 +507,10 @@ import useRepositoryFilters from '@/composables/useRepositoryFilters' export default { components: { RepositoriesFilters, RepositoriesSortBy, RepositoriesList }, props: { - user: { type: String } + user: { + type: String, + required: true + } }, setup(props) { const { user } = toRefs(props) diff --git a/src/guide/composition-api-setup.md b/src/guide/composition-api-setup.md index 29bc1d7456..5db7d0eb5b 100644 --- a/src/guide/composition-api-setup.md +++ b/src/guide/composition-api-setup.md @@ -34,7 +34,7 @@ export default { However, because `props` are reactive, you **cannot use ES6 destructuring** because it will remove props reactivity. ::: -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. +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: ```js // MyBook.vue @@ -48,6 +48,20 @@ setup(props) { } ``` +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`: + +```js +// MyBook.vue + +import { toRef } from 'vue' + +setup(props) { + const title = toRef(props, 'title') + + console.log(title.value) +} +``` + ### Context The second argument passed to the `setup` function is the `context`. The `context` is a normal JavaScript object that exposes three component properties: