gpt4 book ai didi

reactjs - 如何使用 typescript 在reactjs中传递数据

转载 作者:行者123 更新时间:2023-12-04 12:39:47 25 4
gpt4 key购买 nike

如何使用 tsx 将对象传递给 reactjs 中的子组件。当我尝试这种方式时出现此错误。我可以在哪里声明类型?Property 'value' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<ShipmentCard>
Cards.tsx

  render() {
return (
<div>
{this.state.card.map((card: any) => (
<ShipmentCard key={card.id} value={card}/>
))}
</div>
);
}


完整的错误信息是:

Type '{ key: any; data: any; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly<{}> & Readonly<{ children?: ReactNode; }>'. Property 'data' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly<{}> & Readonly<{ children?: ReactNode; }>'

最佳答案

如果您使用的是 typescript ,那么您必须在 ShipmentCard.tsx 中声明所有 Prop

interface ShipmentCardProps {
key: string;
value: {}
}

interface ShipmentCardState {}

class ShipmentCard extends React.Component<ShipmentCardProps, ShipmentCardState> {
}
export default ShipmentCard

关于reactjs - 如何使用 typescript 在reactjs中传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57348905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com