Skip to content

Commit

Permalink
fix: reduce chance of $fetch/$fetchState collision
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed May 22, 2020
1 parent 509ea8d commit 534dc43
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 7 additions & 3 deletions docs/helpers/useFetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export default defineComponent({
setup() {
const name = ref('')

const { $fetch, $fetchState } = useFetch(async () => {
const { fetch, fetchState } = useFetch(async () => {
name.value = await axios.get('https://myapi.com/name')
})

// Manually trigger a refetch
$fetch()
fetch()

// Access fetch error, pending and timestamp
$fetchState
fetchState

return { name }
},
Expand All @@ -33,3 +33,7 @@ export default defineComponent({
::: warning
`useFetch` must be called synchronously within `setup()`. Any changes made to component data - that is, to properties _returned_ from `setup()` - will be sent to the client and directly loaded. Other side-effects of `useFetch` hook will not be persisted.
:::

::: tip
`$fetch` and `$fetchState` will already be defined on the instance - so no need to return `fetch` or `fetchState` from setup.
:::
12 changes: 9 additions & 3 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ async function serverPrefetch(vm: AugmentedComponentInstance) {
setup() {
const name = ref('')
const { $fetch, $fetchState } = useFetch(async () => {
const { fetch, fetchState } = useFetch(async () => {
name.value = await axios.get('https://myapi.com/name')
})
// Manually trigger a refetch
$fetch()
fetch()
// Access fetch error, pending and timestamp
$fetchState
fetchState
return { name }
},
Expand Down Expand Up @@ -178,6 +178,8 @@ export const useFetch = (callback: Fetch) => {

if (process.server || !isSsrHydration(vm))
return {
fetch: vm.$fetch,
fetchState: vm.$fetchState,
$fetch: vm.$fetch,
$fetchState: vm.$fetchState,
}
Expand All @@ -192,6 +194,8 @@ export const useFetch = (callback: Fetch) => {
vm.$fetchState.error = data._error

return {
fetch: vm.$fetch,
fetchState: vm.$fetchState,
$fetch: vm.$fetch,
$fetchState: vm.$fetchState,
}
Expand All @@ -211,6 +215,8 @@ export const useFetch = (callback: Fetch) => {
})

return {
fetch: vm.$fetch,
fetchState: vm.$fetchState,
$fetch: vm.$fetch,
$fetchState: vm.$fetchState,
}
Expand Down

1 comment on commit 534dc43

@vercel
Copy link

@vercel vercel bot commented on 534dc43 May 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.