Next.js 使用 Suspense 进行数据获取。

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

Next.js fetch with Suspense

问题

根据文档,这是一个典型的带有Suspense模式的fetch(有点简化)。

import { getArtist, getArtistAlbums, type Album } from './api';

export default async function Page({
  params: { username },
}: {
  params: { username: string };
}) {
  const albumData = getArtistAlbums(username);

  return (
    <>
      <Suspense fallback={<div>Loading...</div>}>
        <Albums promise={albumData} />
      </Suspense>
    </>
  );
}

// Albums组件
async function Albums({ promise }: { promise: Promise<Album[]> }) {
  // 等待相册承诺解析
  const albums = await promise;

  return (
    <ul>
      {albums.map((album) => (
        <li key={album.id}>{album.name}</li>
      ))}
    </ul>
  );
}

实际上它可以正常工作,但Visual Studio Code一直在抱怨

<Albums promise={albumData} />

因为

它的返回类型'Promise<Element>'不是有效的JSX元素。

我是否遗漏了一些明显的东西?这是我可以在某种程度上消除的Lint错误吗?或者可能是版本问题? 关于正确方向的任何提示都将不胜感激。

英文:

According to the docs, this is a typical fetch with Suspense pattern (a bit simplified).-

import { getArtist, getArtistAlbums, type Album } from &#39;./api&#39;;
 
export default async function Page({
  params: { username },
}: {
  params: { username: string };
}) {
  const albumData = getArtistAlbums(username);
 
 
  return (
    &lt;&gt;
      &lt;Suspense fallback={&lt;div&gt;Loading...&lt;/div&gt;}&gt;
        &lt;Albums promise={albumData} /&gt;
      &lt;/Suspense&gt;
    &lt;/&gt;
  );
}
 
// Albums Component
async function Albums({ promise }: { promise: Promise&lt;Album[]&gt; }) {
  // Wait for the albums promise to resolve
  const albums = await promise;
 
  return (
    &lt;ul&gt;
      {albums.map((album) =&gt; (
        &lt;li key={album.id}&gt;{album.name}&lt;/li&gt;
      ))}
    &lt;/ul&gt;
  );
}

It actually works fine, but Visual Studio Code keeps complaining on

&lt;Albums promise={albumData} /&gt;

due to

Its return type &#39;Promise&lt;Element&gt;&#39; is not a valid JSX element.

Am I missing something obvious here? Is this a Lint error that I can somehow silence? Or maybe a versions issue? Any tip on the right direction is much appreciated.

答案1

得分: 1

The NextJS documentation states that this is a known TypeScript error that is being worked on. From the docs here:

Async Server Component TypeScript Error

An async Server Components will cause a 'Promise<Element>' is not a valid JSX element type error where it is used.
This is a known issue with TypeScript and is being worked on upstream.
As a temporary workaround, you can add {/* @ts-expect-error Async Server Component */} above the component to disable type checking for it.

英文:

The NextJS documentation states that this is a known TypeScript error that is being worked on. From the docs here:

> Async Server Component TypeScript Error
>
> An async Server Components will cause a 'Promise<Element>' is not a valid JSX element type error where it is used.
This is a known issue with TypeScript and is being worked on upstream.
As a temporary workaround, you can add {/* @ts-expect-error Async Server Component */} above the component to disable type checking for it.

huangapple
  • 本文由 发表于 2023年5月17日 15:27:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269517.html
匿名

发表评论

匿名网友

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

确定