如何使用打字稿在reactjs中传递数据
How to pass data in reactjs using typescript
如何使用 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; }>'
尝试this。你 'card' 在你的州吗?好像没有。您需要从父组件获取道具。您需要尝试使用道具传递数据。所以阅读link。并尝试使用 redux。
如果您使用的是打字稿,那么您必须在 ShipmentCard.tsx
中声明所有道具
interface ShipmentCardProps {
key: string;
value: {}
}
interface ShipmentCardState {}
class ShipmentCard extends React.Component<ShipmentCardProps, ShipmentCardState> {
}
export default ShipmentCard
如何使用 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; }>'
尝试this。你 'card' 在你的州吗?好像没有。您需要从父组件获取道具。您需要尝试使用道具传递数据。所以阅读link。并尝试使用 redux。
如果您使用的是打字稿,那么您必须在 ShipmentCard.tsx
中声明所有道具interface ShipmentCardProps {
key: string;
value: {}
}
interface ShipmentCardState {}
class ShipmentCard extends React.Component<ShipmentCardProps, ShipmentCardState> {
}
export default ShipmentCard