Skip to content
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

docs: fix recommended syntax for getters as reactive data sources #2569

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions src/api/reactivity-utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ Can also be used to create a ref for a property on a source reactive object. The
useSomeFeature(toRef(() => props.foo))
</script>
```
When the composable implements accepts a `MaybeRefsOrGetter<T>` through [`toValue`](#tovalue) it is possible to directly use a getter without allocating unnecessary intermediate refs:

```vue
<script setup>
const props = defineProps(/* ... */)

// more efficient and succinct
useSomeFeature(() => props.foo)
</script>
``````


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#computed) with `get` and `set` instead. See the guide to [using `v-model` with components](/guide/components/v-model) for more information.

Expand Down Expand Up @@ -167,8 +178,13 @@ This can be used in [Composables](/guide/reusability/composables.html) to normal
useFeature(1)
useFeature(ref(1))
useFeature(() => 1)

// Using props in this composable with a getter function
useFeature(() => props.id)
```



## toRefs() {#torefs}

Converts a reactive object to a plain object where each property of the resulting object is a ref pointing to the corresponding property of the original object. Each individual ref is created using [`toRef()`](#toref).
Expand Down