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

2023-12-23

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

卡.tsx

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

完整的错误消息是:

输入 '{ 键:任意;数据:任意; }' 不可分配给类型 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly & Readonly'。属性“数据” 类型“IntrinsicAttributes &”上不存在 IntrinsicClassAttributes & Readonly & Readonly'


如果您使用打字稿,那么您必须在 ShipmentCard.tsx 中声明所有道具

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

interface ShipmentCardState {}

class ShipmentCard extends React.Component<ShipmentCardProps, ShipmentCardState> {
}
export default ShipmentCard
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用typescript在reactjs中传递数据 的相关文章

随机推荐