From 471fbf9841aa188a948fe8c2339560be9cb7ba48 Mon Sep 17 00:00:00 2001 From: Billy Schonenberg Date: Tue, 14 Nov 2023 12:09:48 +0100 Subject: [PATCH 1/2] Update reactivity-utilities.md update example by recommended syntax --- src/api/reactivity-utilities.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/reactivity-utilities.md b/src/api/reactivity-utilities.md index 7732864fce..f1742c8f62 100644 --- a/src/api/reactivity-utilities.md +++ b/src/api/reactivity-utilities.md @@ -124,7 +124,7 @@ Can also be used to create a ref for a property on a source reactive object. The useSomeFeature(toRef(props, 'foo')) // getter syntax - recommended in 3.3+ - useSomeFeature(toRef(() => props.foo)) + useSomeFeature(() => props.foo) ``` From acaab7a6bfb90f9c17dda0c2a2497d2357b981a2 Mon Sep 17 00:00:00 2001 From: Billy Schonenberg Date: Tue, 14 Nov 2023 13:19:32 +0100 Subject: [PATCH 2/2] Update documentation --- src/api/reactivity-utilities.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/api/reactivity-utilities.md b/src/api/reactivity-utilities.md index f1742c8f62..b93439ca9e 100644 --- a/src/api/reactivity-utilities.md +++ b/src/api/reactivity-utilities.md @@ -124,9 +124,20 @@ Can also be used to create a ref for a property on a source reactive object. The useSomeFeature(toRef(props, 'foo')) // getter syntax - recommended in 3.3+ - useSomeFeature(() => props.foo) + useSomeFeature(toRef(() => props.foo)) ``` + When the composable implements accepts a `MaybeRefsOrGetter` through [`toValue`](#tovalue) it is possible to directly use a getter without allocating unnecessary intermediate refs: + + ```vue + + `````` + 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. @@ -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).