返回

Next.js使用MUI报错Add the "use client" directive at the top of the file to use it.

2023-11-05 Next.js MUI use client 1322 0

在Next.js中使用MUI会报错:

TypeError: createContext only works in Client Components. Add the "use client" directive at the top of the file to use it.

import Button from "@mui/material/Button";

export default function Home() {
  return (
    <div>
      <Button variant="contained">Hello World</Button>
    </div>
  );
}

解决方法是加上"use client"标签,在import的上方加入,比如:

"use client"
import Button from "@mui/material/Button";

这里要导入的按钮正在使用客户端钩子,在本例中为 createContext。 为此,需要在文件顶部添加“use client”。 但这意味着该页面成为客户端组件,而不再是服务器组件了。

您可能感兴趣:

阿里云 云服务器 99元1年 2核2G 3M固定带宽 续费与新购同价

DOVE 网络加速器 梯子 免费 试用

椤堕儴