React Hooks无法在Firebase Cloud Functions上运行错误:始终违规:无效的Hook调用

Arc*_*oob 5 firebase reactjs google-cloud-functions next.js react-hooks

我正在使用Firebase Cloud Functions托管我的SSR Nextjs应用程序。我可以在Firebase云功能上部署Nextjs应用程序,并使用干净的URL来访问它,而无需使用React Hooks,但是使用React Hooks,我会收到一条错误消息:

Invariant Violation: 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
Run Code Online (Sandbox Code Playgroud)

GitHub firebase / firebase-functions存储库中也报告了此问题

还有一个可重现的示例仓库来测试错误(在pages / about.tsx文件中实现的useState挂钩)。

这是在React:16.10.2上完成的

小智 1

该问题表面上源于 React 源代码的两份副本。我不是 100% 确定它是如何工作的,所以我什至不会尝试解释问题的根本原因。

要解决此问题,请将应用程序放入functions文件夹中并删除package.json。不幸的是,应用程序的所有依赖项都必然是函数的依赖项。然后,改成next.config.js这样:

'use strict';

module.exports = { distDir: './next' };
Run Code Online (Sandbox Code Playgroud)

对此index.js

const dir = __dirname + "/app" const app = next({ dev, dir, conf: {distDir: 'next'} })
Run Code Online (Sandbox Code Playgroud)