Nuxt

nuxt/fetch-composable-in-handler

Call useFetch and useAsyncData only in setup, plugins or composables; in event handlers use $fetch.

umiarkowanastatycznefe-nuxt

Dlaczego

useFetch and useAsyncData register state with the current component and the page payload. Called from a click or submit handler, there is no setup context: each call creates new refs nobody watches, keys collide, results are cached when they should not be, and warnings about the missing Nuxt instance appear.

Poprawka

Use $fetch in event handlers (await $fetch('/api/orders', { method: 'POST', body })), then update your state or call refresh() of a useFetch declared in setup. Wrapping useFetch in your own use... composable is fine, as long as that composable is called from setup.

Przykłady

// bad
async function onSubmit() {
  const { data } = await useFetch('/api/orders', { method: 'POST', body: form })
}
// good
const { data: orders, refresh } = await useFetch('/api/orders')
async function onSubmit() {
  await $fetch('/api/orders', { method: 'POST', body: form })
  await refresh()
}

Źródła

Fakty

Identyfikator
nuxt/fetch-composable-in-handler
Waga
umiarkowana
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
vue jsx
Tylko w projektach z
nuxt
Uczy jej skill
fe-nuxt Nuxt apps
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain nuxt/fetch-composable-in-handler