Getting Unhandled Runtime Error, Error: "undefined" is not valid JSON in when importing Client component to server component

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

Getting Unhandled Runtime Error, Error: "undefined" is not valid JSON in when importing Client component to server component

问题

I have tried to implement a simple counter component that will increase the count of the state variable when a button is clicked, so I made a separate file Counter.tsx and placed it into the component folder in the root of the next project. Here is the code for the Counter.tsx file:

"use client";

import { useState } from "react";

export default function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
}

But when I import this component into the main page.tsx file, I get this strange error:

Unhandled Runtime Error
Error: "undefined" is not valid JSON

Call Stack
React
updateDehydratedSuspenseComponent
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (16088:0)
updateSuspenseComponent
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (15788:0)
beginWork$1
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (17103:0)
beginWork
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (25615:0)
performUnitOfWork
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (24465:0)
workLoopSync
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (24181:0)
renderRootSync
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (24146:0)
performConcurrentWorkOnRoot
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (23250:0)
workLoop
node_modules\next\dist\compiled\scheduler\index.js (10:3921)
flushWork
node_modules\next\dist\compiled\scheduler\index.js (10:3629)
MessagePort.performWorkUntilDeadline
node_modules\next\dist\compiled\scheduler\index.js (10:1811)

What did I do wrong here? I have been trying to find a solution for a few hours.

英文:

I have trying to implement a simple counter component that will increase the count of the state variable when a button is clicked, so i made a separate file Couter.tsx and placed into the component folder in the root of the next project. Here is the code for the Counter.tsx file -

&quot;use client&quot;;

import { useState } from &quot;react&quot;;

export default function Counter() {
  const [count, setCount] = useState(0);

  return (
    &lt;div&gt;
      &lt;p&gt;You clicked {count} times&lt;/p&gt;
      &lt;button onClick={() =&gt; setCount(count + 1)}&gt;Click me&lt;/button&gt;
    &lt;/div&gt;
  );
}

But when i import this component into the main page.tsx file, I get this strange error -

Unhandled Runtime Error
Error: &quot;undefined&quot; is not valid JSON

Call Stack
React
updateDehydratedSuspenseComponent
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (16088:0)
updateSuspenseComponent
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (15788:0)
beginWork$1
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (17103:0)
beginWork
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (25615:0)
performUnitOfWork
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (24465:0)
workLoopSync
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (24181:0)
renderRootSync
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (24146:0)
performConcurrentWorkOnRoot
node_modules\next\dist\compiled\react-dom\cjs\react-dom.development.js (23250:0)
workLoop
node_modules\next\dist\compiled\scheduler\index.js (10:3921)
flushWork
node_modules\next\dist\compiled\scheduler\index.js (10:3629)
MessagePort.performWorkUntilDeadline
node_modules\next\dist\compiled\scheduler\index.js (10:1811)

What did I do wrong here? I have been trying to find a solution for a few hours.

答案1

得分: 1

您的组件代码运行良好,这里是您应用程序的Stackblitz复制链接

请注意,导入可以使用别名,上面的示例使用了相对导入路径。

英文:

Your component code works just fine, here is a Stackblitz reproduction of your app.

Note that imports can use aliases, the example above uses a relative import path.

答案2

得分: 0

没有错误在我的代码中,原来是我的互联网连接那时候不工作。这段代码在其他有正常网络连接的电脑上运行正常。我只是浪费了一天的时间修复了我代码中不存在的问题。

英文:

There was no error in my code, it turns out that my internet connection was not working at that time. The code runs find on other computers which have a proper network connection. I just wasted 1 day fixing the non existent problem in my code.

huangapple
  • 本文由 发表于 2023年4月19日 21:32:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055172.html
匿名

发表评论

匿名网友

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

确定