UseFetch() calling multiple request, only the last data is fetched and $fetch send requests twice
See original GitHub issueEnvironment
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>
<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…
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:
- Created 10 months ago
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I use
url + JSON.stringify(params) for useAsyncData,
, but with hash look’s nicetry to use a unique key (include in it query params for example)