0

I've built this simple project with react-query and nextjs , after running the project it gives me this error in the console, what did i do wrong ?!

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'QueryClient'
    |     property 'queryCache' -> object with constructor 'QueryCache'
    |     property 'queries' -> object with constructor 'Array'
    |     ...
    |     index 0 -> object with constructor 'QueryObserver'
....

the _app.js file:

import { QueryClient, QueryClientProvider } from "react-query";

const queryClient = new QueryClient();

function MyApp({ Component, pageProps }) {
  return (
    <QueryClientProvider client={queryClient}>
      <Component {...pageProps} />
    </QueryClientProvider>
  );
}

export default MyApp;

my custom hook:

import axios from "axios";
import { useQuery } from "react-query";
async function fetchUserData() {
  const res = await axios.get("https://jsonplaceholder.typicode.com/users");
  return res;
}

export function useUserData() {
  return useQuery("userData", fetchUserData);
}

and my index.js file

import { useUserData } from "../hooks/useUserData";
export default function Home() {
  const { data, isLoading, error } = useUserData();
  const users = data?.data;
  console.log("userData", data);
  return (
    <div>
      {isLoading && <div>Next is Loading ...</div>}
      {users?.map((user) => (
        <div key={user.email}>
          <div>{user.name}</div>
          <div>{user.username}</div>
          <div>{user.email}</div>
        </div>
      ))}
    </div>
  );
}

here is the repo for the source code: https://github.com/ascode94/next-simple-fetching

1 Answer 1

1

There is a browser extension that tries to serialize everything that is in react context to json, i think it’s: https://chrome.google.com/webstore/detail/react-context-devtool/

So it tries to serialize the queryClient and that fails. Disabling it or excluding the ReactQueryClientProvider (if possible) should fix this

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

3 Comments

Thanks TkDodo, i have installed this extension and it was the source of the problem ! Thank you So much
What? So you have to make all of your users download an extension to avoid the error? Can you elaborate a bit. Im assuming im missing something here.... ?
no, it's the other way around. you only get an error IF you have that specific extension installed, because it tries to serialize everything that is in a react-context to JSON. However, you can have things in context that are not json serializable, which react-query happens to do.

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.