Mähh
Automatic pending state for data being fetched?
its similar like in the
useAsyncData
https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/asyncData.ts#L395-L398
30 replies
Automatic pending state for data being fetched?
Do i get it correctly, you wan't some logic like the default from
useAsyncData
where you don't only have the Promise, instead also can access the fields like data
, error
, status
and so on without the Promise is done?30 replies
Automatic pending state for data being fetched?
Checked the source code:
https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/asyncData.ts#L251C5-L257C6
This is what happens behind
useFetch
and useAsyncData
you could use that to get all the status
references and return a bool to know if any useFetch or useAsyncData is pending / refreshing.
For the custom $fetch
here is an example for the plugin way:
https://nuxt.com/docs/guide/recipes/custom-usefetch30 replies
Automatic pending state for data being fetched?
plugin would be more elegant way to inject $myFetch globally.
Anyways, i think you got the point. Have a state which is aware of pending requests in you application, by intercept the requests by its core methods 😄
30 replies
Automatic pending state for data being fetched?
what about a custom $fetch wrapper? (btw. your are right: useFetch only should be used for getting data, not for update or create requests, for that
$fetch
should be used, thats because of internal caching logic behind useAsyncData / useFetch i guess)
Anyways.
30 replies
UseState with a state coming from a db
I created an issue for it:
https://github.com/nuxt/nuxt/issues/28169
10 replies
UseState with a state coming from a db
https://stackblitz.com/edit/nuxt-starter-zckkxt
I'm with you, it should be prevent the fetching, at least for simple cases like:
But only setting a
key
option, will prevent duplicated requests, when used in multiple components.10 replies
UseState with a state coming from a db
useFetch
does not prevent multiple fetches, if you use that composable in e.g. 4 components it will fetch 4 times.
You can prevent that by define the cachedata:
This will make sure your fetch does not trigger multiple times. But you need to call the refresh()
method, in case you want to load the data again.
From docs:
useFetch
is the same as
Just with "auto key generation" based on URL and options passed for the request.
---
If you rely on multiple global states, i would recommend pinia for store / state management:
https://nuxt.com/modules/pinia
Stores are in the nuxt context ,so they are builded very similar to composables itself.
stores/my-store.ts
But you cannot initialize the state itself in the store. Since the hydration of the state between SSR und CSR are after the execution / initialization of the store itself.
I handle that with middlewares whenever i have global states which are e.g. delivered by an api:
middlewares/context.global.ts
10 replies
State not changing when using a composables
Wait, even if props are reactive, not every value in props is reactive.
So if you pass
props.playerId
to a composable, the value of it within the composable is not reactive anymore.
If props.playerId change within the component, the composable still keeps the old value, so the watcher in your composable:
will not trigger, since only the props
is reactive, not the props.playerId
itself. So if you pass const foo = myComposable(props.playerId)
you do not get the changes.
You need to pass actually a ref
/ computed
or the whole props object to keep the reactivity across your component and composable.13 replies