Skip to content

Commit 1cc7db4

Browse files
committed
docs: add addendum on useId usage with computed
1 parent a3c31de commit 1cc7db4

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/api/composition-api-helpers.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ This is the underlying helper that powers [`defineModel()`](/api/sfc-script-setu
4242
set?: (v: T) => any
4343
}
4444
45-
type ModelRef<T, M extends PropertyKey = string, G = T, S = T> = Ref<G, S> & [
46-
ModelRef<T, M, G, S>,
47-
Record<M, true | undefined>
48-
]
45+
type ModelRef<T, M extends PropertyKey = string, G = T, S = T> = Ref<
46+
G,
47+
S
48+
> &
49+
[ModelRef<T, M, G, S>, Record<M, true | undefined>]
4950
```
5051

5152
- **Example**
@@ -132,3 +133,20 @@ Used to generate unique-per-application IDs for accessibility attributes or form
132133
IDs generated by `useId()` are also guaranteed to be stable across the server and client renders, so they can be used in SSR applications without leading to hydration mismatches.
133134

134135
If you have more than one Vue application instance of the same page, you can avoid ID conflicts by giving each app an ID prefix via [`app.config.idPrefix`](/api/application#app-config-idprefix).
136+
137+
:::warning Caution
138+
`useId()` should be not be called inside a `computed()` property due to potential instance conflicts. Instead, it should be declared outside of `computed()` before the value is called inside of the computed function.
139+
140+
```vue
141+
<script setup>
142+
import { computed, useId } from 'vue'
143+
144+
const id = useId()
145+
146+
const modifiedId = computed(
147+
() => `${id}-${Math.floor(Math.random() * 10)}`
148+
)
149+
</script>
150+
```
151+
152+
:::

0 commit comments

Comments
 (0)