0

here is the code I am using. it doesn't make any sense to me because the error is basically telling me I am not passing it a function when using query but I am. and I am not calling the function directly either. any help would be a appreciated

import logo from "./logo.svg";
import "./App.css";
import { useEffect, useState } from "react";
//import Posts from './components/Posts'
import axios from "axios";
import { useQuery } from "react-query";
import React from "react";

async function fetchPosts() {
  const data = await fetch("http://swapi.dev/api/planetsd");
  return data.json();
}

function App() {
  const { data, status, error } = useQuery(
    "stuffthing",
    async () => await fetchPosts()
  );
  // first argumello\'ent is a string to cache and track the query result
  if (status === "error") {
    console.log(`${error} new error`);
  }
  if (status === "loading") {
    return <div>loading</div>;
  }

  return (
    <div className="container">
      <h1>Posts</h1>
      {data}
    </div>
  );
}

export default App;
4
  • Please edit your question to include the actual error message, in full please Commented Jul 31, 2022 at 23:57
  • You appear to be using the deprecated react-query library. You should be using @tanstack/react-query instead Commented Aug 1, 2022 at 0:12
  • I'm most likely following the same tutorial as you, and got exactly the same problem. Commented Aug 1, 2022 at 14:13
  • @PaoloTedesco did you use newest react-query? Commented Aug 1, 2022 at 17:18

1 Answer 1

6

I just ran into the exact same issue and it took me wayyy too long to figure out what was going on. If you're following the documentation of v3, useQuery would indeed be used as such:

const { data, status, error } = useQuery("posts", async () => await fetchPosts());

However, in v4 it has changed to take an array as a first parameter:

const { data, status, error } = useQuery(["posts"], async () => await fetchPosts());

Three hours of my life I'll never get back.

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

4 Comments

I was going to answer this, the method expects different parameters across react-query versions. @Phil, I just run npm install react-query (not @tanstack/react-query) and I got version 4 installed.
@PaoloTedesco ah, looks like the authors have set up an alias or something like that
@Phil it could be... given that I'm following a tutorial on the topic, I'm not the most qualified person to answer :)
Looks like react-query is stuck at version 4.0.0. You should have seen a deprecation warning when installing it and should migrate immediately to @tanstack/react-query

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.