Update variable on UInput change
How do I make Nuxt UI update a
ref
whenever the UInput is changed? I tried using v-model
and @input
but it doesn't work. I'm showing the current value of the ref
in a UCard
Expected behavior:
User types a value into the input
The value shown in the card updates
Actual behavior:
User types into input
Card does not update
Code:
15 Replies
Hi @Eternal. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To update a variable when using Nuxt UI's
If
- Use only
UInput
, you should use the v-model
directive. This will automatically keep your ref
or reactive
variable in sync with the input value. You do not need to use the @input
event handler for this purpose—v-model
is sufficient and recommended.
Key points:
- v-model
on UInput
will update your variable reactively as the user types.
- If your variable is a ref
or a property of a reactive
object, the UI will update automatically wherever you use that variable, such as inside a UCard
.
Example from the Nuxt UI documentation:
As the user types in the input, the value in the card updates immediately, thanks to Vue's reactivity and v-model
binding on UInput
Nuxt UI Input Usage.
In your code:
You are using v-model="modifyingAnnouncementFormState.calendarEventId"
on UInput
, which is correct. However, you are also using an @input
handler that manually sets the value, which is unnecessary and may cause issues. Remove the @input
handler:
Note:If
modifyingAnnouncementFormState
is a ref
holding an object, you should access its value as modifyingAnnouncementFormState.value.calendarEventId
in the script, but in the template, Vue automatically unwraps refs, so your usage is correct.
Summary:- Use only
v-model
for two-way binding with UInput
.
- Remove the @input
handler.
- The card will update automatically as the input changes.
For more details, see the official Nuxt UI Input documentation here.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@705244213414985758> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai
Why doesn't attaching a
watch()
work? The value updates now, but watch doesn't do anything
Let me check the knowledge base for you again. One moment! :books:
----------------------
The reason your
Use a getter function in your watcher to track changes to properties inside a ref object. This is a common pattern in Vue 3 and Nuxt 3 for ensuring reactivity with nested properties. If you need to watch a property of a reactive object or a ref holding an object, always use a function as the first argument to
- Vue Ref no longer reactive after being returned from composable - Nuxt 3/Vue 3 Reactivity issue, while rendering component in v-for loop ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
watch()
is not working as expected is likely due to how Vue's reactivity system works with refs and objects. If modifyingAnnouncementFormState
is a ref
holding an object, then modifyingAnnouncementFormState.calendarEventId
is not a ref itself—it's just a property on the object. Vue cannot deeply track changes to properties inside a ref object unless you watch the whole object or use a deep watcher.
From the knowledge sources:
- If you want to watch a property inside a ref object, you should use a getter function in your watcher, like this:
This ensures Vue tracks the correct dependency and triggers the watcher when calendarEventId
changes Vue Ref no longer reactive after being returned from composable.
- Watching a property directly (e.g., modifyingAnnouncementFormState.calendarEventId
) will not work if that property is not a ref itself, as Vue's reactivity system won't track it in this way Nuxt 3/Vue 3 Reactivity issue, while rendering component in v-for loop.
Summary:Use a getter function in your watcher to track changes to properties inside a ref object. This is a common pattern in Vue 3 and Nuxt 3 for ensuring reactivity with nested properties. If you need to watch a property of a reactive object or a ref holding an object, always use a function as the first argument to
watch()
.
---
References:- Vue Ref no longer reactive after being returned from composable - Nuxt 3/Vue 3 Reactivity issue, while rendering component in v-for loop ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@705244213414985758> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai
It only works if the variable I'm watching is a ref, not a reactive
Let me check the knowledge base for you again. One moment! :books:
----------------------
You're correct: in Vue 3 (and therefore Nuxt 3), if you want to watch a property inside a
ref
that holds an object, you need to use a getter function in your watcher, like () => myRef.value.someProp
. This works because Vue can track the dependency chain through the getter.
However, if you use a reactive
object, you can watch its properties directly, but you must also use a getter function. For example:
If you try to watch state.foo
directly (without the getter), it won't work, because Vue expects a ref, a reactive object, or a getter function as the first argument to watch
(see example).
So, whether you're using a ref
holding an object or a reactive
object, you should always use a getter function in the watcher to ensure reactivity:
- For a ref object: watch(() => myRef.value.someProp, ...)
- For a reactive object: watch(() => myReactive.someProp, ...)
This is a common source of confusion, but it's the expected behavior in Vue 3 and Nuxt 3 (see discussion).
If you still find that watching a property of a reactive
object doesn't work, double-check that you're using a getter function as the first argument to watch
. If you use the property directly, Vue will not track it reactively.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@705244213414985758> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai
It doesn't work even if I use a getter for watching a reactive
Let me check the knowledge base for you again. One moment! :books:
----------------------
I'm sorry, but the knowledge sources provided do not contain explicit information about why watching a property of a
reactive
object with a getter might not work in Nuxt 3 or Vue 3. The sources do confirm that using a getter function is the correct approach for watching properties inside both ref
and reactive
objects, and that direct property watching does not work for nested properties in a ref
object Nuxt 3/Vue 3 Reactivity issue, while rendering component in v-for loop.
However, if you are experiencing issues even when using a getter with a reactive
object, the knowledge sources do not provide a specific explanation or troubleshooting steps for this scenario. If you can provide a minimal reproducible example, it may help clarify the issue, but based on the current sources, I cannot give a more detailed answer.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@705244213414985758> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai
Code for reproduction:
Let me check the knowledge base for you again. One moment! :books:
----------------------
Based on the knowledge sources, your usage of
- Your watcher syntax is correct. - The knowledge sources do not explain why it would not work with a
reactive
and the watcher appears correct:
However, if the watcher is not firing when you update calendarEventId
via v-model
on UInput
, the knowledge sources do not provide a specific explanation for this issue with Nuxt UI's UInput
and a reactive
object.
The sources do confirm that using a getter function in the watcher is the correct approach for both ref
and reactive
objects, and that Vue's reactivity system should track such changes Nuxt 3/Vue 3 Reactivity issue, while rendering component in v-for loop, Toggle modal when changing text prop dynamically. There is no mention of a known issue with reactive
and UInput
in the provided sources.
If this is not working in your case, the knowledge sources do not contain enough information to explain why. It may be related to how Nuxt UI's UInput
interacts with reactive
objects, but this is not documented in the sources above.
Summary:- Your watcher syntax is correct. - The knowledge sources do not explain why it would not work with a
reactive
object and UInput
.
- There is no explicit workaround or known issue documented in the sources provided.
If you need a guaranteed reactive update, using a ref
for modifyingAnnouncementFormState
and accessing its properties via .value
is a common pattern, but this is not explicitly stated in the sources above.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@705244213414985758> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
This issue has not been solved
It works when using a ref but it doesn't work when using a reactive
Please ping me when you respond