diff --git a/docs/content/2.guide/2.features/5.data-fetching.md b/docs/content/2.guide/2.features/5.data-fetching.md index 96b7f4354c..73de0a94ba 100644 --- a/docs/content/2.guide/2.features/5.data-fetching.md +++ b/docs/content/2.guide/2.features/5.data-fetching.md @@ -6,60 +6,6 @@ Nuxt provides `useFetch`, `useLazyFetch`, `useAsyncData` and `useLazyAsyncData` **`useFetch`, `useLazyFetch`, `useAsyncData` and `useLazyAsyncData` only work during `setup` or `Lifecycle Hooks`** :: -## `useAsyncData` - -Within your pages, components and plugins you can use `useAsyncData` to get access to data that resolves asynchronously. - -::ReadMore{link="/api/composables/use-async-data"} -:: - -### Example - -```ts [server/api/count.ts] -let counter = 0 -export default () => { - counter++ - return JSON.stringify(counter) -} -``` - -```vue [app.vue] - - - -``` - -:LinkExample{link="/examples/composables/use-async-data"} - -## `useLazyAsyncData` - -This composable behaves identically to `useAsyncData` with the `lazy: true` option set. In other words, the async function does not block navigation. That means you will need to handle the situation where the data is `null` (or whatever value you have provided in a custom `default` factory function). - -::ReadMore{link="/api/composables/use-lazy-async-data"} -:: - -### Example - -```vue - - - -``` - ## `useFetch` Within your pages, components and plugins you can use `useFetch` to universally fetch from any URL. @@ -114,6 +60,66 @@ watch(posts, (newPosts) => { ``` +## `useAsyncData` + +Within your pages, components and plugins you can use `useAsyncData` to get access to data that resolves asynchronously. + +::alert +You might be asking yourself: what is the difference between `useFetch` and `useAsyncData`? + +In brief, `useFetch` receives a URL and gets that data, whereas `useAsyncData` might have more complex logic. `useFetch(url)` is nearly equivalent to `useAsyncData(url, () => $fetch(url))` - it's developer experience sugar for the most common use case. +:: + +::ReadMore{link="/api/composables/use-async-data"} +:: + +### Example + +```ts [server/api/count.ts] +let counter = 0 +export default () => { + counter++ + return JSON.stringify(counter) +} +``` + +```vue [app.vue] + + + +``` + +:LinkExample{link="/examples/composables/use-async-data"} + +## `useLazyAsyncData` + +This composable behaves identically to `useAsyncData` with the `lazy: true` option set. In other words, the async function does not block navigation. That means you will need to handle the situation where the data is `null` (or whatever value you have provided in a custom `default` factory function). + +::ReadMore{link="/api/composables/use-lazy-async-data"} +:: + +### Example + +```vue + + + +``` + ## Refreshing Data Sometimes throughout the course of your user's page visit, you may need to refresh the data loaded from the API. This can happen if the user chooses to paginate, filter results, search, etc.