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

标签 reactjs components pass-data tsx

如何使用 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/

相关文章:

swift - 从单元格到新 View Controller 的图像传输通常会加载错误的图像,有什么方法可以解决这个问题吗?

javascript - "Error watching file for changes: EMFILE"当初始化一个 react-native 项目时

javascript - 在 React 中嵌套组件, Prop 不传递给子组件

reactjs - 如何知道react-router是否可以返回并在react应用程序中显示后退按钮

java - 如何为我的 Java 应用程序创建一个类似于 Netbeans 起始页的起始页?

delphi - 如何 "scan"当前安装的 VCL 组件的完整列表

ios - 如何将数据从 DetailView 传递到 TableView?

ios - 从 JSON 获取值并传递给另一个 View

javascript - 生产中的 Webpack : Why React Native errors?

javascript - React Admin 如何使用基本身份验证?在我的示例中,Auth header 很好地发送到入口点,但未发送到所有其他端点