简体   繁体   English

错误:对象在 Next.js 的应用程序目录中作为 React 子项无效

[英]Error: Objects are not valid as a React child in the app directory of Next.js

I started migrating one of my Next.js applications to version 13. I wanted to use the app directory with Server and Client Components.我开始将我的 Next.js 应用程序之一迁移到版本 13。我想将app目录与服务器和客户端组件一起使用。 The documentation says that: 文件说:

Server and Client Components can be interleaved in the same component tree.服务器和客户端组件可以交错在同一个组件树中。 Behind the scenes, React will merge the work of both environments.在幕后,React 将合并两个环境的工作。

But my below attempt fails with this error:但是我的以下尝试失败并出现此错误:

Error: Objects are not valid as a React child (found: [object Promise]).错误:对象作为 React 子项无效(找到:[object Promise])。 If you meant to render a collection of children, use an array instead.如果您打算渲染子集合,请改用数组。

"use client";

import Comments from "./Comments";

const Post = () => {
  return (
    <div>
      <p>This is a post</p>
      <Comments />
    </div>
  );
};

export default Post;
export default async function Comments() {
  const res = await fetch("https://jsonplaceholder.typicode.com/comments");
  return <div></div>;
}

I have looked around the Inte.net and Stack Overflow but didn't find an answer.我查看了 Inte.net 和 Stack Overflow,但没有找到答案。 Any help would be appreciated.任何帮助,将不胜感激。

If you continued a little bit through the documentation , you would have seen that they talk about a restriction when it comes to Render, a Server Component inside a Client Component:如果您继续阅读文档,您会看到他们在谈到 Render(客户端组件中的服务器组件)时谈到了一个限制:

However, in React, there's a restriction around importing Server Components inside Client Components because Server Components might have server-only code (eg database or filesystem utilities).但是,在 React 中,在客户端组件中导入服务器组件存在限制,因为服务器组件可能只有服务器代码(例如数据库或文件系统实用程序)。

For example, importing a Server Component in a Client Component will not work:例如,在客户端组件中导入服务器组件将不起作用:

'use client';

// ❌ This pattern will not work. You cannot import a Server
// Component into a Client Component
import ServerComponent from './ServerComponent';

export default function ClientComponent() {
  return (
    <>
      <ServerComponent />
    </>
  );
}

Instead, you can pass a Server Component as a child or prop of a Client Component.相反,您可以将服务器组件作为客户端组件的子组件或道具传递。 You can do this by wrapping both components in another Server Component.您可以通过将这两个组件包装在另一个服务器组件中来做到这一点。 For example:例如:

'use client';

export default function ClientComponent({children}) {
  return (
    <>
      {children}
    </>
  );
}
// ✅ This pattern works. You can pass a Server Component
// as a child or prop of a Client Component.
import ClientComponent from "./ClientComponent";
import ServerComponent from "./ServerComponent";

// Pages are Server Components by default
export default function Page() {
  return (
    <ClientComponent>
      <ServerComponent />
    </ClientComponent>
  );
}

Following these guidelines, in your case, you should pass Comments as children to Post , like so:按照这些准则,在您的情况下,您应该将Comments作为children项传递给Post ,如下所示:

"use client";

const Post = ({ children }) => {
  return (
    <div>
      <p>This is a post</p>
      {children}
    </div>
  );
};

export default Post;
<Post>
  <Comments/>
</Post>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM