‘Promise<Element>’ 不是有效的JSX元素

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

Next 13 — client and async server component combined: 'Promise<Element>' is not a valid JSX element

问题

  1. 我在实验性的 Next 13 `app` 目录中进行实验。
  2. 根据[文档][1],客户端组件可以接受服务器端组件,只要作为 `children` 属性。
  3. 它能工作,但是我遇到了类型错误
  4. &gt; '&#39;ServerPage&#39;' 不能用作 JSX 组件。它的返回类型 '&#39;Promise&lt;Element&gt;&#39;' 不是有效的 JSX 元素。
  5. &gt; 类型 '&#39;Promise&lt;Element&gt;&#39;' 缺少类型 '&#39;ReactElement&lt;any, any&gt;&#39;' 的以下属性:typepropskey
  6. import ClientPage from './clientPage';
  7. import ServerPage from './serverPage';
  8. // app/page.tsx
  9. export default function Home() {
  10. // 在服务器中记录日志
  11. console.log('渲染首页');
  12. return (
  13. &lt;&gt;
  14. &lt;ClientPage&gt;
  15. &lt;ServerPage /&gt; // 这里有类型错误
  16. &lt;/ClientPage&gt;
  17. &lt;/&gt;
  18. );
  19. }
  20. **serverPage.tsx** 组件
  21. const fetchSomeData = async () => {
  22. const response = await fetch('https://pokeapi.co/api/v2/pokemon/ditto');
  23. if (!response.ok) {
  24. throw new Error(response.statusText);
  25. }
  26. return response.json();
  27. };
  28. export default async function ServerPage() {
  29. const data = await fetchSomeData();
  30. // 这在服务器中记录日志
  31. console.log(data);
  32. return (
  33. &lt;&gt;
  34. &lt;main className={styles.main}&gt;
  35. 我是一个服务器组件
  36. &lt;/main&gt;
  37. &lt;/&gt;
  38. );
  39. }
  40. **clientPage.tsx** 组件
  41. '在客户端使用';
  42. export default function ClientPage({
  43. children,
  44. }: {
  45. children: React.ReactNode;
  46. }) {
  47. // 这在客户端记录日志
  48. console.log('渲染客户端页面');
  49. return (
  50. &lt;&gt;
  51. {children}
  52. &lt;/&gt;
  53. );
  54. }
  55. 我猜这与 `clientPage` 组件中的 children 类型有关,但我不确定应该如何对其进行类型化。
  56. [1]: https://beta.nextjs.org/docs/rendering/server-and-client-components#importing-server-components-into-client-components
英文:

I was playing with the experimental Next 13 app directory.

As per the documents, a client component can accept a server component, as long as it is as a children prop.

It works, however I do get a type error

> 'ServerPage' cannot be used as a JSX component. Its return type
> 'Promise<Element>' is not a valid JSX element.
> Type 'Promise<Element>' is missing the following properties from type 'ReactElement<any, any>': type, props, key

  1. import ClientPage from &#39;./clientPage&#39;;
  2. import ServerPage from &#39;./serverPage&#39;;
  3. // app/page.tsx
  4. export default function Home() {
  5. // logs in the server
  6. console.log(&#39;rendering home page&#39;);
  7. return (
  8. &lt;&gt;
  9. &lt;ClientPage&gt;
  10. &lt;ServerPage /&gt; // type error here
  11. &lt;/ClientPage&gt;
  12. &lt;/&gt;
  13. );
  14. }

the serverPage.tsx component

  1. const fetchSomeData = async () =&gt; {
  2. const response = await fetch(&#39;https://pokeapi.co/api/v2/pokemon/ditto&#39;);
  3. if (!response.ok) {
  4. throw new Error(response.statusText);
  5. }
  6. return response.json();
  7. };
  8. export default async function ServerPage() {
  9. const data = await fetchSomeData();
  10. // this logs in the server
  11. console.log(data);
  12. return (
  13. &lt;&gt;
  14. &lt;main className={styles.main}&gt;
  15. Im a server component
  16. &lt;/main&gt;
  17. &lt;/&gt;
  18. );
  19. }

the clientPage.tsx component

  1. &#39;use client&#39;;
  2. export default function ClientPage({
  3. children,
  4. }: {
  5. children: React.ReactNode;
  6. }) {
  7. // this logs in the client
  8. console.log(&#39;rendering client page&#39;);
  9. return (
  10. &lt;&gt;
  11. {children}
  12. &lt;/&gt;
  13. );
  14. }

I suppose that this has to do with the children type in the clientPage component, but I'm not sure how this should be typed.

答案1

得分: 4

Async Server Component TypeScript Error

要使用 TypeScript 来创建异步服务器组件,请确保你的 TypeScript 版本高于 5.1.3,同时使用 @types/react 版本高于 18.2.8。

如果你使用的 TypeScript 版本较旧,可能会出现 "'Promise<Element>'" 不是有效的 JSX 元素类型错误。更新到最新版本的 TypeScript 和 @types/react 应该解决此问题。

next.config.js

  1. /** @type {import('next').NextConfig} */
  2. const nextConfig = {
  3. experimental: { serverActions: true },
  4. };
  5. module.exports = nextConfig;
英文:

Async Server Component TypeScript Error

To use an async Server Component with TypeScript, ensure you are using TypeScript 5.1.3 or higher and @types/react 18.2.8 or higher.

If you are using an older version of TypeScript, you may see a 'Promise<Element>' is not a valid JSX element type error. Updating to the latest version of TypeScript and @types/react should resolve this issue.

next.config.js

  1. /** @type {import(&#39;next&#39;).NextConfig} */
  2. const nextConfig = {
  3. experimental: { serverActions: true },
  4. };
  5. module.exports = nextConfig;
  6. ``
  7. </details>
  8. # 答案2
  9. **得分**: 1
  10. 抱歉代码部分不需要翻译以下是已翻译的内容
  11. "Unfortunately, the only way around this at the moment is to use the `:any` type definition on the async server components. `Next.js 13` outlined this in their [Typescript documentation][1].
  12. > Warning: You can use async/await in layouts and pages, which are Server Components. Using async/await inside other components, with TypeScript, can cause errors from the response type from JSX. You might see the error 'Promise&lt;Element&gt;' is not a valid JSX element. We are tracking [this issue here][2].
  13. [1]: https://beta.nextjs.org/docs/configuring/typescript
  14. [2]: https://github.com/vercel/next.js/issues/42292"
  15. <details>
  16. <summary>英文:</summary>
  17. Unfortunately, the only way around this at the moment is to use the `:any` type definition on the async server components. `Next.js 13` outlined this in their [Typescript documentation][1].
  18. &gt; Warning: You can use async/await in layouts and pages, which are Server Components. Using async/await inside other components, with TypeScript, can cause errors from the response type from JSX. You might see the error &#39;Promise&lt;Element&gt;&#39; is not a valid JSX element. We are tracking [this issue here][2].
  19. [1]: https://beta.nextjs.org/docs/configuring/typescript
  20. [2]: https://github.com/vercel/next.js/issues/42292
  21. </details>
  22. # 答案3
  23. **得分**: 0
  24. 这是一个错误Next.js团队已经在处理你也可以使用eslint注释
  25. &lt;ClientPage&gt;
  26. // @ts-expect-error 服务器端组件
  27. &lt;ServerPage /&gt; // 这里有类型错误
  28. &lt;/ClientPage&gt;
  29. <details>
  30. <summary>英文:</summary>
  31. This is a bug. Next.js team has been working on it. You could also use eslint comment
  32. &lt;ClientPage&gt;
  33. // @ts-expect-error Server Component
  34. &lt;ServerPage /&gt; // type error here
  35. &lt;/ClientPage&gt;
  36. </details>

huangapple
  • 本文由 发表于 2023年2月19日 09:25:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497449.html
匿名

发表评论

匿名网友

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

确定