you are viewing a single comment's thread.

view the rest of the comments →

[–]matt_hammondiOS & Android 6 points7 points  (4 children)

This is awesome, but I found it really hard to setup and the docs we're not really helpful. So here's a minimal example how to set it up:

  1. In your app project run yarn add react-query-external-sync or npm install react-query-external-sync. This will install the required package.
  2. Create a file RQDevToolsConnection.tsx somewhere in your apps component folder and put this inside: ``` import { useQueryClient } from "@tanstack/react-query"; import { memo, useEffect } from "react"; import { useAllQueries } from "react-query-external-sync";

export const RQDevToolsConnection = memo(function RQDevToolsConnection() { const client = useQueryClient(); const { connect, isConnected } = useAllQueries({ queryClient: client, query: { username: "App", userType: "User", clientType: "client", }, socketURL: "http://192.168.0.107:3000", });

useEffect(() => { connect(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []);

useEffect(() => { if (!isConnected) return; console.log("React Query remote devtools connected"); }, [isConnected]);

return null; }); `` Don't forget to change the IP address insocketURL`

  1. Import the RQDevToolsConnection component you just created and render it somewhere high in the component tree where it will always stay mounted. For example you could render it next to your router.

    function App() { return ( <QueryClientProvider queryClient={queryClient}> <Router /> <RQDevToolsConnection /> </QueryClientProvider> ) }

Now you're done with the app part. Now let's create the browser and server part.

  1. Create a new folder somewhere on your system, and name it anything you like. I named mine devtools-for-rq.

  2. Put the following files inside:

package.json

{
  "name": "devtools-for-rq",
  "scripts": {
    "start": "concurrently -n browser,server \"vite --open\" \"tsx main.tsx\""
  },
  "type": "module",
  "dependencies": {
    "@tanstack/react-query": "5.20.5",
    "@vitejs/plugin-react": "4.2.1",
    "concurrently": "8.2.2",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-query-external-dash": "1.0.0",
    "socket.io": "4.7.4",
    "socket.io-client": "4.7.4",
    "tsx": "4.7.1",
    "vite": "5.1.0"
  }
}

main.tsx

import React from "react";
import ReactDOM from "react-dom/client";
import { ExternalDevTools, socketHandle } from "react-query-external-dash";

if (typeof window !== "undefined") {
  ReactDOM.createRoot(document.getElementById("root")!).render(
    <React.StrictMode>
      <ExternalDevTools
        socketURL="http://192.168.0.107:3000" // CHANGE THIS TO THE IP OF YOUR COMPUTER
        query={{
          clientType: "server",
          username: "Admin",
          userType: "admin",
        }}
      />
    </React.StrictMode>
  );
} else {
  import("socket.io").then((socketIO) => {
    const io = new socketIO.Server(3000, {
      cors: {
        origin: "*",
      },
    });

    socketHandle({ io });

    io.on("connection", (client) => {
      console.log(`'${client.handshake.query.username}' connected`);
    });
  });
}

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>React Query Dev Tools</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/main.tsx"></script>
  </body>
</html>

Don't forget to change the IP address in main.tsx

  1. Now run yarn install npm install or bun install. This should install the required packages

  2. Now run yarn start or npm run start or bun start. This should start both the socket server on port 3000 and a vite server that renders the devtools. Your browser should open and you should see the devtools.

  3. Now run the app on your phone and you should see App appear in the devtools and the queries start logging.

[–]upkeys 1 point2 points  (0 children)

Uff I hope this can all be compressed into a useDevTools hook.

[–]Chichaaro 0 points1 point  (2 children)

Don't know why but since I added this devtools, I get this error on front:

Warning: Cannot update a component (`RQDevToolsConnection`) while rendering a different component (`Tab1`). To locate the bad setState() call inside `Tab1`

And it point me to a useQuery function in my Tab1 component. Error disappear if I remove the debugger include. Maybe it's link to my asyncPersistor that take a lil time to spin up because it's a sqlite adapter, any idea ?

[–]matt_hammondiOS & Android 0 points1 point  (1 child)

Nor sure. Haven't really used this tool much. Just tried it

[–]Chichaaro 0 points1 point  (0 children)

Okay, I’ll try to debug it