Skip to content

Clarify the usage of toRefs and toRef for optional props #639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/api/refs-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
25 changes: 20 additions & 5 deletions src/guide/composition-api-introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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: '' }
Expand Down Expand Up @@ -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([])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion src/guide/composition-api-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down