question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useFetch with url function does not re-fetch data

See original GitHub issue

Environment

Nuxt CLI v3.0.0-rc.8 13:40:47 Nuxt project info: 13:40:49


  • Operating System: Linux
  • Node Version: v18.6.0
  • Nuxt Version: 3.0.0-rc.8
  • Package Manager: yarn@1.22.19
  • Builder: vite
  • User Config: build, buildModules, runtimeConfig
  • Runtime Modules: -
  • Build Modules: @nuxtjs/tailwindcss@5.3.2

Reproduction

components/ProjectName.vue:

<template>
  <span>
  	{{props.projectId}}
  	<span>{{project}}</span>
  </span>
</template>

<script setup>
const props = defineProps({
	projectId: String
})

const projectId = ref(props.projectId)

const { data: project, pending, refresh } = await useFetch(
	() => {  
		const url = '/api/projects/' + projectId.value; 
		console.log(url);
		return url
	}, {
	  headers: useRequestHeaders(['cookie']),
	  watch: [projectId],
	  lazy: false,
	  cache: false
	})

</script>

pages/[projectId].vue

<template>
<ProjectName :projectId=$route.params.projectId />
</template>

Describe the bug

If “projectId” prop of the ProjectName.vue component changes, the useFetch url() generation function is called and writes the new url via console.log(url) to console. However, no new data is fetched from the backend.

Additional context

Might be related to https://github.com/nuxt/framework/issues/6273

Logs

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
AndersNielsen85commented, Sep 1, 2022

@bf You might consider a shorter “workaround” with useFetch. Adding the key made it work in my case at least. 😃

But agreed the documentation says it should refresh using a function

const { data } = await useFetch('/api/projects/' + projectId.value, { key: projectId.value });

0reactions
panle666commented, Sep 7, 2022

@bf您可能会考虑使用 useFetch 使用更短的“解决方法”。 添加密钥使其至少在我的情况下工作。😃

但同意文档说它应该使用函数刷新

const { data } = await useFetch('/api/projects/' + projectId.value, { key: projectId.value });

@bf您可能会考虑使用 useFetch 使用更短的“解决方法”。 添加密钥使其至少在我的情况下工作。😃

但同意文档说它应该使用函数刷新

const { data } = await useFetch('/api/projects/' + projectId.value, { key: projectId.value });

nice

Read more comments on GitHub >

github_iconTop Results From Across the Web

useFetch do not refetch data when use Dynamic Routes #6604
useFetch do not refetch data when use Dynamic Routes #6604 ... dynamic route should be a function that returns a string. const {...
Read more >
useFetch
Need to fetch some data? Try this one out. It's an isomorphic fetch hook. That means it works with SSR (server side rendering)....
Read more >
Custom useFetch Hook not working after url state change
Here is the first component that calls useFetch effectively and returns data: const ArtistDeck = (props) => { // props.apiRequest is a ...
Read more >
useFetch - VueUse
Reactive Fetch API provides the ability to abort requests, intercept requests before they are fired, automatically refetch requests when the url changes, and ......
Read more >
useFetch() react hook - usehooks-ts
The fetch is executed when the component is mounted and if the url changes. ... is unmounted before the data is recovered, the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found