简体   繁体   English

使用 typescript + 生成的核心用作 package 调用 graphql-codegen 查询时,挂钩调用无效

[英]Invalid hook calls when calling a graphql-codegen query with typescript + generated core used as a package

I'm using Yarn workspaces with a npm package having the generated code and a mobile package that uses it.我正在使用带有生成代码的 npm package 和使用它的移动 package 的 Yarn 工作区。

When trying to make a call from an imported query I get an invalid hook call.尝试从导入的查询中拨打电话时,我收到无效的挂钩调用。 Using any other hook in that component (ie useState) works fine.在该组件中使用任何其他钩子(即 useState)都可以正常工作。

My package.json of my mobile package imports: "@workspace-library/core": "1.0.0",我的手机 package 的 package.json 导入: "@workspace-library/core": "1.0.0",

I have an index.js file at core/src that has:我在 core/src 有一个 index.js 文件,它有:

export * from "./generated/graphql";
export { apollo, useAuth, withAuth };

And I import the queries like this:我像这样导入查询:

import {
  useArticlesQuery
} from "@workspace-library/core";

const SidebarArticles: FunctionComponent<{ props: any }> = (props: any) => {
  const [test, setTest] = useState("false");
  const data = useArticlesQuery({
    fetchPolicy: "no-cache",
    notifyOnNetworkStatusChange: true,
  });

   return (
    <View>
      <Text>Heyhey</Text>
    </View>
  );
};

I can see the useArticlesQuery in my generated/graphql.jsx, just in case:我可以在生成的/graphql.jsx 中看到 useArticlesQuery,以防万一:

export function useArticlesQuery(baseOptions) { return ApolloReactHooks.useQuery(ArticlesDocument, baseOptions); }

Environment:环境:

  • OS: Ubuntu操作系统:Ubuntu
  • "@graphql-codegen/cli": "^1.13.1", "@graphql-codegen/cli": "^1.13.1",
  • "@graphql-codegen/typescript": "^1.13.1", "@graphql-codegen/typescript": "^1.13.1",
  • "@graphql-codegen/typescript-operations": "^1.13.1", "@graphql-codegen/typescript-operations": "^1.13.1",
  • "@graphql-codegen/typescript-react-apollo": "^1.13.1", "@graphql-codegen/typescript-react-apollo": "^1.13.1",
  • Yarn workspaces纱线工作区

Additional context Error in question:有问题的附加上下文错误:

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
* /home/dogchef/Development/workspace-library/packages/core/node_modules/react/cjs/react.development.js:1589:4 in resolveDispatcher
* /home/dogchef/Development/workspace-library/packages/core/node_modules/react/cjs/react.development.js:1597:29 in useContext
* http://192.168.1.2:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false:181621:41 in useBaseQuery
* /home/dogchef/Development/workspace-library/packages/core/node_modules/@apollo/react-hooks/lib/react-hooks.cjs.js:550:18 in useQuery
* http://192.168.1.2:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false:197883:37 in useArticlesQuery
* components/SidebarArticles.tsx:10:2 in SidebarArticles

I run into the same issue.我遇到了同样的问题。 Using useQuery is working though.使用useQuery是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用@graphql-codegen/cli 在 graphql 查询中获取子类型 - get sub types in graphql query using @graphql-codegen/cli 调用自定义钩子时无效的钩子调用 - Invalid Hook Call when calling a custom hook React - 使用 package 时挂钩调用无效 - React - Invalid hook call when using package 与npm软件包一起使用时,无效的propTypes警告 - Invalid propTypes warning when used with a npm package ReactJS 中的无效挂钩调用 - Invalid Hook Calls In Validation in ReactJS 用于初始化 useState 钩子的传入属性在调用钩子的更新程序方法时得到更新 - Passed-in prop used to initialize a useState hook gets updated when calling the hook's updater method 错误:无效的挂钩调用。 在等待之后调用钩子时 - 你如何等待来自一个钩子的数据实例化到另一个钩子? - Error: Invalid hook call. when calling hook after await - how do you await for data from one hook to instantiate into another? 类型问题:在 typescript 中使用 contextAPI 时,如何将 useState(react-hook) 用作全局变量? - Type Problem: How can useState(react-hook) be used as a global variable when contextAPI is used in typescript? Graphql 调用 Axios 多次调用性能/重构 - Graphql Calling Axios Multiple Calls Performance/Refactoring react.js应用程序中的无效钩子调用 - Invalid hook calls in react.js application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM