next.js 为什么在接下来,js“使用客户端“在组件顶部给予我这个错误?

2j4z5cfb  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(176)

当我在我的组件的顶部使用“use client”时,我得到这个错误:

event - compiled client and server successfully in 2.5s (265 modules)
wait  - compiling...
event - compiled client and server successfully in 932 ms (265 modules)
wait  - compiling /page (client and server)...
event - compiled client and server successfully in 2.5s (531 modules)
wait  - compiling...
event - compiled client and server successfully in 1131 ms (531 modules)
error - SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
wait  - compiling /favicon.ico/route (client and server)...
event - compiled successfully in 455 ms (300 modules)`

对于此代码:

`      "dependencies": {
        "eslint": "8.38.0",
        "eslint-config-next": "13.3.0",
        "next": "13.3.0",
        "react": "18.2.0",
        "react-dom": "18.2.0"
      }`
'use client'
export default function Home() {
  console.log("====================================");
  console.log("Test");
  console.log("====================================");
  return (
    <>
      <h1>Next.js</h1>
    </>
  );
}

我尝试使用client-side-render,也出现错误

yhuiod9q

yhuiod9q1#

令人惊讶的是,您不能将"use client"指令与export default function F () {}一起设置,而是使用const F = () => {}; export default F。这很奇怪,但这种变通方法对我很有效。祝你好运!

"use client"

const Home = () => {
  console.log("====================================");
  console.log("Test");
  console.log("====================================");

  return (
    <>
      <h1>Next.js</h1>
    </>
  );
};

export default Home;

相关问题