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() calling multiple request, only the last data is fetched and $fetch send requests twice

See original GitHub issue

Environment

rc-13 node-18

Reproduction

//composable

const myfetch = async (url,options,emits) => {
  const { data, pending, error, refresh } = await useFetch(url, {
 
    ...options,
  baseURL: useRuntimeConfig().API_URL, 
  initialCache: false,
  headers: new Headers({  'authorization': (process.client)?localStorage.getItem("token"):''}),
  onResponse ({ response }) {
  return response._data
  },
  onResponseError ({ response }) {
  return response._data
  }
  })
  const resData = data.value
 
  return resData
  }

//component 1

<script setup>
getAllItems();
async function getAllItems() {
    let response = await myfetch('/article/artPosts?' +
        'page=' + page.value + '&' +
        'artPostGroup=' + artPostGroup.value + '&' +
        'limit=' + limit.value, {
            method: 'GET'
        });
    if (response.data)
        list.value = response.data.docs;
    pages.value = response.data.pages;
}
</script>

//component 2

<script setup>
getAllItems();
async function getAllItems() {
    let response = await myfetch('/store/storeCourses?' +
        'page=' + page.value + '&' +
        'storeCourseGroup=' + storeCourseGroup.value + '&' +
        'limit=' + limit.value, {
            method: 'GET'
        });
    if (response.data)
        list.value = response.data.docs;
    pages.value = response.data.pages;
}
</script>

//home.vue

<div>
<component1></component1>
<component2></component2>
</div>

Describe the bug

the bug is here… in the page load: response of component1 is the same as response of component2… image

when i use $fetch instead of useFetch, it works good but it makes another problem for me…

on refreshing page,it send a request twice to server

Additional context

how can i write function in composables?

Logs

No response

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
mittcicommented, Nov 12, 2022

i read this and it works fine 😃 thank you @Maxp777

import { hash } from ‘ohash’ and send key with this code ‘key’ : hash([‘api-fetch’, url, params]),

I use url + JSON.stringify(params) for useAsyncData,, but with hash look’s nice

1reaction
mittcicommented, Nov 12, 2022

try to use a unique key (include in it query params for example)

Options (from useAsyncData): key: a unique key to ensure that data fetching can be properly de-duplicated across requests, if not provided, it will be generated based on the static code location where useAyncData is used.

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Fetch API requesting multiple get requests
getElementById('overview') );. Is this possible? If not, what are other solutions to fetching multiple JSON data into my rendering ReactDOM ...
Read more >
useFetch
Stops making more requests if there is no more data to fetch. (i.e. if we have 25 todos, and the perPage is 10,...
Read more >
Why is my fetch getting called twice? : r/reactjs
It shows it getting called twice in the console, but only shows once rendered on the page. import React, { useEffect, useState }...
Read more >
How to Fetch Data From an API Using React Hooks
In this article, we will look at how to fetch data from API using React hooks and also how to use the data...
Read more >
SWR: React Hooks for Data Fetching
SWR is a React Hooks library for data fetching. SWR first returns the data from cache (stale), then sends the fetch request (revalidate),...
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