gpt4 book ai didi

reactjs - React with Typescript - Type { } 缺少类型中的以下属性

转载 作者:行者123 更新时间:2023-12-03 13:42:03 24 4
gpt4 key购买 nike

我正在尝试使用 TypeScript 学习 React,但似乎一直遇到有点模糊的 TS 错误。

我在下面的三个文件中编写了代码,编译和运行时工作正常。我不断收到 TypeScript 抛出的错误,这非常烦人

"Type '{ id: any; key: any; }' is missing the following properties from type 'ProfileCardProps': login, name"

//表单.tsx

import * as React from 'react';
import Axios from 'axios';

export default class Form extends React.Component<any,any>{
constructor(props: any){
super(props);
this.state = { userName: ""};
}

handleSubmit = (event: React.FormEvent<EventTarget>): void => {
event.preventDefault();

//get request...
.then(resp => {
this.props.onSubmit(resp.data);
console.log(resp.data);
this.setState({userName: ''});
};

public render() {
return(
<div>
<div className="col-sm-12">
<form onSubmit={this.handleSubmit}>
<label>Run lookup:<br />
<input type="text"
value = {this.state.userName}
onChange = {(event) => this.setState({userName: event.target.value})}
placeholder = "Enter Username..." >
</input>
</label>
<button type="submit">Add user info</button>
</form>
<br />
</div>
</div>
);
};

}

//Card.tsx

import * as React from 'react';

interface ProfileCardProps{
login: string;
name: string;
}

const ProfileCard = (props: ProfileCardProps) => {

return(
<div className="card col-xs-1 col-sm-6 col-md-4 col-lg-3">
<div className="profileWrapper">
<div className="userName">
<p>{props.login}</p>
</div>
<div className="user">
<h3>{props.name}</h3>
</div>
</div>
</div>
)
}

const CardList = (props: { cards: { map: (arg0: (card: any) => JSX.Element) => React.ReactNode; }; }) => {
return(
<div className="row">

// This is the line that is throwing the error
{props.cards.map((card: { id: any; }) => <ProfileCard key={card.id} {...card} />)}
</div>
)
}

export default CardList;

//个人资料列表

import * as React from 'react';
import Form from './Form';
import CardList from './ProfileCard';

import "./ProfileStyles.scss";

export default class Profiles extends React.Component<any, any>{
state = {
cards: [
{ login: "exampleLogin",
name:"exampleName",
key: 1
},
{ login: "exampleLogin2",
name:"exmapleName2",
key: 2
}
]
}

addNewCard = (cardInfo: any) => {
this.setState((prevState: { cards: { concat: (arg0: any) => void; }; }) => ({
cards: prevState.cards.concat(cardInfo)
}));
}


public render() {
return(
<div className="cardSection">
<h2>Profiles</h2>
<Form onSubmit={this.addNewCard} />
<CardList cards={this.state.cards} />
</div>
)
};
}

最佳答案

当您将卡片传递给 ProfileCard 组件时,它会在 props 中传递 4 个值。

{login: string, name: string, key: number, id: number}

但是你的界面只有2个

interface ProfileCardProps{ 
login: string;
name: string;
}

添加 key 和 ID 应该可以解决问题。

interface ProfileCardProps{ 
login: string;
name: string;
id:any;
key: any;
}

关于reactjs - React with Typescript - Type { } 缺少类型中的以下属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55133907/

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