【发布时间】:2021-04-22 04:00:51
【问题描述】:
当我在 Next.js 应用程序上使用挂钩时,我收到 'Error: Invalid hook call'。我不知道为什么我会收到这个错误,因为我认为它是在函数组件中调用的?
Server Error
Error: 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://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Source
pages/index.js (16:44) @ getStaticProps
14 |
15 | export const getStaticProps = async () => {
> 16 | const { data, loading, error } = useQuery(LINGO_QUERY);
| ^
17 |
18 | return {
19 | props: {
在此处查看我的代码框:https://codesandbox.io/s/empty-sea-hjunv
【问题讨论】:
-
它不是一个函数组件,你不能在那里使用钩子,我想你应该调用 fetch。
-
我正在使用 Apollo,所以我不需要使用 fetch?
-
你使用了 apollo hooks,所以你只能在函数组件中使用它们。
标签: reactjs react-hooks next.js