承诺< element>不是有效的JSX元素,可以转换Promise< element>到元素
我正在尝试使用item
组件,但是它给出了此错误:
(alias) const Item: (id: string) => Promise<JSX.Element>
import Item
Type '{}' is not assignable to type 'string'.ts(2322)
'Item' 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 ts(2786)
项目组件是这样的:
const Item = async (id: string) => {
const itemData = await getItemData(id);
return (<Button className="item">
<ItemContent name={itemData.name} description={itemData.description} />
</Button>);
}
getItemdata
函数是这样:
async function getItemData(item: string) {
if (item === "") {
return {
name: "Error",
description: "Couldn't fetch the latest items :( ..."
}
}
const itemData = fetch("/api/items/getItem/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: item
})
})
.catch(err => {
console.error(err);
throw new Error("Failed to get item data");
});
const res = (await itemData).json();
return res;
}
我该怎么做才能转换promise&lt; element&gt;
to element
,我尝试了。我尝试键入函数,它也不起作用,每当我尝试使用
item
组件时,它就会出现错误。
如果很重要,我将使用react next.js与打字稿
I'm trying to use the Item
component, but it gives this error:
(alias) const Item: (id: string) => Promise<JSX.Element>
import Item
Type '{}' is not assignable to type 'string'.ts(2322)
'Item' 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 ts(2786)
The Item component is like this:
const Item = async (id: string) => {
const itemData = await getItemData(id);
return (<Button className="item">
<ItemContent name={itemData.name} description={itemData.description} />
</Button>);
}
The getItemData
function is like this:
async function getItemData(item: string) {
if (item === "") {
return {
name: "Error",
description: "Couldn't fetch the latest items :( ..."
}
}
const itemData = fetch("/api/items/getItem/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: item
})
})
.catch(err => {
console.error(err);
throw new Error("Failed to get item data");
});
const res = (await itemData).json();
return res;
}
What can I do to convert Promise<Element>
to Element
, I tried .then(res => res.json())
, it doesn't work, I tried typing the functions, it doesn't work either, whenever I try to use the Item
component it just gives the error.
If it's important, I'm using React Next.js with Typescript
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要与TypeScript一起使用异步服务器组件,请确保您使用Typescript 5.1.3或更高版本以及 @types/eack 18.2.8或更高版本。
它来自官方下一个文档
另外我还添加typescript角色:
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.
it is from official next documentation
Also I add typescript role before component:
使用此命令安装最新版本的Typescript和 @type/react:
它将解决问题。
安装后,请确保重新启动您的代码编辑器。这解决了问题。
另外,如果组件在顶部具有“使用客户端”,则将其删除。
Install the latest version of typescript and @types/react with this command:
it'll fix the issues.
After the installation make sure to restart your code editor. This solved the issue.
Also if the component has "use client" at the top remove it.
确保您在page.tsx中使用
getItemdata
函数,这是因为JSX或TSX函数不支持异步。在这里,我给您一个示例,假设搜索是从参数完成的Make sure you are using the
GetItemData
function in the page.tsx, this is because the JSX or TSX functions do not support asynchrony. Here I give you an example assuming that the search is done from the parameter