Skip to content

Update API entry for toRef to mention mutating props #1624

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
Mar 21, 2022
Merged
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
8 changes: 6 additions & 2 deletions src/api/reactivity-utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ Can be used to create a ref for a property on a source reactive object. The crea
const fooRef = ref(state.foo)
```

The above ref is **not** synced with `state.foo`, because the `ref()` receives a plain string value.
The above ref is **not** synced with `state.foo`, because the `ref()` receives a plain number value.

`toRef()` is useful when you want to pass the ref of a prop to a composable function:

```vue
<script setup>
import { toRef } from 'vue'

const props = defineProps(/* ... */)

// convert `props.foo` into a ref, then pass into
Expand All @@ -94,7 +96,9 @@ Can be used to create a ref for a property on a source reactive object. The crea
</script>
```

`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).
When `toRef` is used with component props, the usual restrictions around mutating the props still apply. Attempting to assign a new value to the ref is equivalent to trying to modify the prop directly and is not allowed. In that scenario you may want to consider using [`computed`](./reactivity-core.html#computed) with `get` and `set` instead. See the guide to [using `v-model` with components](/guide/components/events.html#usage-with-v-model) for more information.

`toRef()` will return a usable ref even if the source property doesn't currently exist. This makes it possible to work with optional properties, which wouldn't be picked up by [`toRefs`](#torefs).

## toRefs()

Expand Down