[英]React/Typescript Property 'ID' does not exist on type 'never'.ts(2339)
I have this React/Typescript page:我有这个 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;
I am getting this error: Property 'ID' does not exist on type 'never'.ts(2339)
on every line that has data.ID
or data.whatever
.我收到此错误:在具有
data.ID
或data.whatever
的每一行Property 'ID' does not exist on type 'never'.ts(2339)
。
Why would those values never be used or are always null?为什么这些值永远不会被使用或者总是 null? I use nearly identical code in a couple other apps so I'm not sure what is different about this situation.
我在其他几个应用程序中使用了几乎相同的代码,所以我不确定这种情况有什么不同。 Thanks in advance.
提前致谢。
You need to add type for playersData
when you decalre it.您需要在贴花时为
playersData
添加类型。
interface DataType {
ID: string;
Handle: string;
Role: string;
}
const [playersData, setPlayersData] = useState<Array<DataType>>([]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.