![](/img/trans.png)
[英]Typescript error: Property 'status' does not exist on type 'never'.ts(2339)
[英]React/Typescript Property 'ID' does not exist on type 'never'.ts(2339)
我有这个 React/Typescript 页面:
import React, { useState, useEffect } from 'react';
import { Page } from './Page';
import { PageTitle } from './PageTitle';
import { Button, Card } from 'react-bootstrap';
function PlayersPage() {
const [isLoading, setIsLoading] = useState(false);
const [playersData, setPlayersData] = useState([]);
//Fetch all players from database
useEffect(() => {
//setIsLoading(true);
fetch('http://localhost:44326/api/Players')
.then((response) => {
if (response.ok) {
return response.json();
}
})
.then((data) => setPlayersData(data));
//.then(setIsLoading(false));
}, []);
return (
<Page>
<div style={{ padding: 20 }}>
<PageTitle>Toons</PageTitle>
</div>
<div>
{playersData.map((data) => (
<div key={data.ID}>
<Card>
<Card.Title>{data.ID}</Card.Title>
<Card.Text>{data.Handle}</Card.Text>
<Card.Body>
<div className="lables">
<Card.Text>Role</Card.Text>
</div>
<div className="lables">
<Card.Text>{data.Role}</Card.Text>
</div>
<Button variant="primary">Work</Button>
</Card.Body>
</Card>
<br></br>
</div>
))}
</div>
</Page>
);
}
export default PlayersPage;
我收到此错误:在具有data.ID
或data.whatever
的每一行Property 'ID' does not exist on type 'never'.ts(2339)
。
为什么这些值永远不会被使用或者总是 null? 我在其他几个应用程序中使用了几乎相同的代码,所以我不确定这种情况有什么不同。 提前致谢。
您需要在贴花时为playersData
添加类型。
interface DataType {
ID: string;
Handle: string;
Role: string;
}
const [playersData, setPlayersData] = useState<Array<DataType>>([]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.