首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当使用类型记录+生成的核心作为包调用graphql-codegen查询时,钩子调用无效

当使用类型记录+生成的核心作为包调用graphql-codegen查询时,钩子调用无效
EN

Stack Overflow用户
提问于 2020-04-04 17:44:29
回答 1查看 482关注 0票数 0

我正在使用Yarn工作区,其中包含有生成代码的npm包和使用它的移动包。

当试图从导入的查询进行调用时,我会得到一个无效的钩子调用。在该组件中使用任何其他挂钩(即useState)都很好。

我的移动包导入的package.json:"@workspace-library/core": "1.0.0",

我在核心/src上有一个index.js文件,它有:

代码语言:javascript
复制
export * from "./generated/graphql";
export { apollo, useAuth, withAuth };

然后我导入如下的查询:

代码语言:javascript
复制
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>
  );
};

我可以在生成的/Graphql.jsx中看到useArticlesQuery,以防万一:

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

环境:

Ubuntu

  • "@graphql-codegen/cli":"^1.13.1",

  • "@graphql-codegen/typescript":"^1.13.1",

  • "@graphql-codegen/typescript-operations":"^1.13.1",

  • "@graphql-codegen/typescript-react-apollo":"^1.13.1",

  • Yarn工作区

有问题的附加上下文错误:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-07-13 17:06:37

我遇到了同样的问题。不过,使用useQuery是可行的。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61032231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档