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 refresh not working when params change

See original GitHub issue

Environment

Nuxt CLI v3.0.0-rc.4 18:15:14 RootDir: /Users/samedwardes/git/embeddedapp/webapp-frontend 18:15:15 Nuxt project info: 18:15:15


  • Operating System: Darwin
  • Node Version: v18.2.0
  • Nuxt Version: 3.0.0-rc.4
  • Package Manager: npm@8.9.0
  • Builder: vite
  • User Config: modules, runtimeConfig
  • Runtime Modules: @nuxtjs/tailwindcss@5.1.2, @formkit/nuxt@1.0.0-beta.8
  • Build Modules: -

👉 Report an issue: https://github.com/nuxt/framework/issues/new 18:15:15

👉 Suggest an improvement: https://github.com/nuxt/framework/discussions/new

👉 Read documentation: https://v3.nuxtjs.org

Reproduction

The example from the docs works.

<script setup>
const page = ref(1);

const { data: users, pending, refresh, error } = await useFetch(() => `users?page=${page.value}&take=6`, { baseURL: config.API_BASE_URL }
);

function previous(){
  page.value--;
  refresh();
}

function next() {
  page.value++;
  refresh();
}
</script>

When I pass page into the params option it does not work.

<script setup>
const page = ref(1);

const { data: users, pending, refresh, error } = await useFetch(() => `users`, {
  baseURL: config.API_BASE_URL ,
  params: { 
    page: page.value,
    take: 6
  }
}
);

function previous(){
  page.value--;
  refresh();
}

function next() {
  page.value++;
  refresh();
}
</script>

The API is still called again, but always with page set to a value of 1.

Describe the bug

The API is still called again, but always with page set to a value of 1.

Additional context

Is this expected behaviour, or is this a bug?

Logs

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:5
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
ddavid67commented, Jul 27, 2022

there should be a way to automaticaly refetch based on a reactive search params this is already the current behevior with the first parameter of usefetch even refresh() does not provide a way to inject new params

what is the purpose of the watch option if the refresh will not catch new values ?

3reactions
danielroecommented, Jul 4, 2022

This is how Vue reactivity works - the value is frozen in time when you call useFetch: see https://github.com/nuxt/framework/issues/4970. If you want to keep it reactive, you can use useAsyncData and provide a custom fetcher function. Something like this: https://stackblitz.com/edit/github-c4jssa.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useFetch of Nuxt 3 sends same request during refresh instead ...
I try to do a simple thing: At route change, refetch data. Here is my link:
Read more >
[Nuxt 3] How the data fetching ( and refreshing ) is supposed ...
I used useFetch but the same parameter is documented for useAsyncData aswell. const { data } = await useFetch('/api/auctions', { initialCache: ...
Read more >
useHooks - Easy to understand React Hook recipes
We bring you easy to understand React Hook code recipes so you can learn how React hooks work and feel more comfortable writing...
Read more >
Data Fetching | Vue Router
Let's assume we have a Post component that needs to fetch the data for a post based on $route.params.id : <template> <div class="post">...
Read more >
Data Fetching - Nuxt
Unlike fetch , this hook does not display a loading placeholder during ... call fetch in your component (to e.g. reload its async...
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