0

The issue is that my view is not re-rendering nor is useFetch re-running when a pagination link is clicked. I suspect this is something to do with data reactivity in Nuxt - which is new to me.

I have this as my script.

<script setup lang="ts">
const route = useRoute();
const { data, status, error, refresh } = await useFetch("/api/search", {
  query: useRoute().query,
});

const page_options = [10, 25, 50, 100];
</script>

The page renders correctly and processes the URL query params of q, per_page and page.

I have an array of links to navigate to the search URL with an updated page param.

 <NuxtLink
          :class="{
            'p-2 border-2': true,
            'bg-pink-800 text-white': index === Number(data.pagination.page),
          }"
          v-for="index in data.pagination.page_count"
          :key="index"
          :to="{
            name: 'archive-search',
            query: {
              per_page: data.pagination.per_page,
              page: index,
              q: route.query.q,
            },
          }"
        >
          {{ index }}
        </NuxtLink>

The URL updates but nothing is trigger either server side or client side. I suspect this is something to do with watching a value but how is this best done with url params?

Cheers.

1 Answer 1

0

According to the official documents of Nuxt, all fetch options can be given a computed or ref value. These will be watched and new requests made automatically with any new values if they are updated.

useRoute().query is not a ref. You can wrap it in a toRef or computed to trigger refetch.

const route = useRoute();
const query = computed(()=>{
  return route.query
});
const { data, status, error, refresh } = await useFetch("/api/search", {
  query,
});

Ref: https://nuxt.com/docs/api/composables/use-fetch#params

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.