Next.JS – "use client" directive produces an error

huangapple go评论56阅读模式
英文:

Next.JS - "use client" directive produces an error

问题

我遇到了一个非常奇怪的Next.js问题。我刚刚创建了一个新的项目,每次我尝试在页面上键入"use client"时,都会遇到一个"SyntaxError: 在JSON的位置0处发现意外的令牌'u'"错误。

供参考,这是我的代码:

"use client"

import { useRouter } from 'next/router';

export default function Page() {
  const router = useRouter();

  return (
    <div>
      <h1>Page</h1>
    </div>
  );
}

我已经尝试过卸载/重新安装create-next-app,但似乎没有帮助。

感谢任何帮助!

英文:

I'm having a super weird next.js issue. I just created a new project and any time I try to type &quot;use client&quot;; on a page, I'm met with a "SyntaxError: Unexpected token u in JSON at position 0".

For reference, here's my code:

&quot;use client&quot;

import { useRouter } from &#39;next/router&#39;;

export default function Page() {
  const router = useRouter();

  return (
    &lt;div&gt;
      &lt;h1&gt;Page&lt;/h1&gt;
    &lt;/div&gt;
  );
}

I've already tried to uninstall/reinstall create-next-app, but it doesn't seem to help.

Thanks for any help!

答案1

得分: 2

"next"最新版本中似乎破坏了“使用客户端”的指令。解决方法是通过npm i next@canary使用最新的canary版本。

英文:

It turns out next broke the "use client" directive in their latest version. The solution is to use the latest canary via npm i next@canary.

huangapple
  • 本文由 发表于 2023年4月20日 10:52:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76060180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定