Have any way to fetch data using tanstack query into a .ts file?
So, i have a createChartFactory.ts that have a lot of functions and one called init, so inside init i want fetch data and return.
` const fetchPricesFx = async () => {
const res = await fetch(
my-api`)
const a = await res.json()
console.log("MODEL PARAMS PRICE", createQueryPrice(model.params))
return a
}
const indicatorPrice = await fetchPricesFx()
const indicatorConfig = createConfig({
indicator: indicator,
params: model.params,
indicatorData: indicatorData,
priceData: indicatorPrice,
})```
it is a .ts file, but when i try change fetch to useQuery i receive a:
Error fetching chart data: Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
Im using next 143 Replies
other-emerald•2y ago
My guess is that you're calling this function outside your react query context provider? You should make sure this function is called within the provider or you'll have to pass in a new queryClient yourself
sensitive-blueOP•2y ago
Hmm, no, I'm pretty sure I'm calling the function inside a provider. It works fine in a .TSX file, but I need it to work in a .TS file
other-emerald•2y ago
Try making a stackblitz reproduction