无法使用挂钩编译 Next.js 项目

Cannot compile Next.js project with hooks

我创建了一个非常简单的 Next.js 项目,它只包含一页,index.js,看起来像:

import React, { useState } from 'react';

const MyComponent = () => {
    const [instance, setInstance] = useState()

    return (
        <></>
    );
};

const Home = () => <>
        <MyComponent />
</>;

export default Home;

当我运行它处于开发模式时,没有错误,一切正常。

但是当我 运行 next build 时,我得到:

Error occurred prerendering page "/": Error: Minified React error #321; visit https://reactjs.org/docs/error-decoder.html?invariant=321 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
> Build error occurred

看起来像是钩子的问题,但我不明白我的实现有什么问题。如果我删除行 const [instance, setInstance] = useState(),构建成功。

我正在使用 next ^9.1.4,react ^16.12.0 和 react-dom ^16.12.0.

你能帮我解决这个问题吗? :)

我只是将您的代码复制到 pages/index.js 和 运行 npm run build(其中包含 next build,并且有效。

下一个 9.1.4 反应,反应-dom 16.12.0

我最好的猜测......你有另一个全局安装?并且 next build 接下来使用它?

或者可能是你安装的lib,你的package.json里有什么包?