medusa
medusa copied to clipboard
Cannot use react-query for custom queries while using medusa-react
Bug report
Describe the bug
Importing and using react-query inside the Gatsby admin while simultaneously using the medusa-react package is not possible.
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 See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
System information
Medusa version (including plugins): Medusa: 1.3.3, medusa-react: 0.3.3, Node.js version: v16.16.0 Database: Postgres 14.4 Operating system: macOS Monterey 12.5
Steps to reproduce the behavior
- Create a new custom route in medusa server with medusa-extender
- Define this custom route in Gastby admin inside src/services/api.js
- Import useQuery from react-query inside a react component
- import Medusa from src/services/api.js inside the same react component
- Call the custom route using useQuery like this:
const { data } = useQuery( "listCollections", () => Medusa.collections.listParentCategories ) - The error is thrown as shown above.
Expected behavior
useQuery should be available to use for custom routes inside Gatsby admin alongside medusa-react.
Additional context
Someone in the discord channel suggested that the problem is caused by 3. You might have more than one copy of React in the same app. I have tried
cd project/node_modules/reactyarn linkcd ../medusa-reactyarn link reactcd ../..yarn run start
This does not resolve the issue. I have also verified that medusa-react has react listed as a peer dependency so that does not seem to be the issue.
Thanks for reporting this one in. @kasperkristensen Do you have an idea as to why this is happening?
Hi @sheikhumar93,
I can't seem to replicate this issue, have you tried deleting your node_modules and reinstalling dependencies?
Hi @sheikhumar93,
I can't seem to replicate this issue, have you tried deleting your node_modules and reinstalling dependencies?
Hello @kasperkristensen. Yes multiple times, tried both npm and yarn as well. I will create a reproducible repo later today and share it over here.
@sheikhumar93 Any update on the reprository?
Do you have a code sample or codesandbox example?
I'm going to go ahead and close this. I've tested it several times and we will soon be releasing an update to the admin dashboard where we use "custom" queries, and it does not result in such errors. If you are still experiencing this error then feel free to re-open the issue and provide a reproducible repo 🙂