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

时间:2019-12-08 11:05:23

标签: reactjs react-hooks next.js

我创建了一个非常简单的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(),则构建成功。

我正在使用下一个^ 9.1.4,反应^ 16.12.0和反应球^ 16.12.0。

您能帮我吗? :)

1 个答案:

答案 0 :(得分:1)

我只是将您的代码复制到pages / index.js并运行npm run build(其中包含next build,就可以了。

下一个9.1.4 反应,反应dom 16.12.0

enter image description here

我的最佳猜测...您是否还要在全球范围内再安装一次?和next build代替下一个?

或者它可能是您安装的库,您的package.json中有什么软件包?

相关问题