useFetch() and watch reactivity
I got a lack of reactivity on a useLazyFetch call:
How come my manual watch() works but the reactive sources in
query
won't trigger another fetching?2 Replies
Hi @enti. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The behavior you're seeing is due to how Nuxt's
With
useFetch
and useLazyFetch
handle reactivity with their options, especially when using immediate: false
.
Key points from the documentation:
- When you provide reactive values (like refs or computeds) in the query
option, Nuxt will automatically watch those and refetch when they change—but only if immediate
is true
(the default). If you set immediate: false
, Nuxt will not fetch automatically on changes to the reactive sources unless the data has already been fetched once. After the first fetch, changes to the watched sources will trigger a refetch, but not before that. This is a change from previous behavior and is now consistent between useFetch
and useAsyncData
Key Change Behavior in useAsyncData and useFetch.
- The watch
option allows you to specify additional reactive sources to watch, but again, with immediate: false
, you need to manually trigger the first fetch (e.g., by calling refresh
or execute
). After that, changes to the watched sources will trigger refetches Key Change Behavior in useAsyncData and useFetch.
- If you want full manual control, you can set watch: false
and handle all refreshing yourself.
Why does your manual watcher work?
Your manual watcher works because it explicitly calls refresh()
whenever the watched values change, regardless of the immediate
setting. This bypasses Nuxt's internal logic and ensures the fetch is triggered exactly when you want.
Summary:With
immediate: false
, Nuxt will not automatically fetch on reactive source changes until the first fetch has occurred. After the first fetch, reactivity works as expected. If you want to ensure fetching happens on every change, you need to either:
- Trigger the first fetch manually, or
- Use your own watcher (as you did), which gives you full control.
For more details, see the official documentation: Key Change Behavior in useAsyncData and useFetch.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@210882495707742219> Kapa.ai is still learning and improving, please let me know how I did by reacting below.