![](/img/trans.png)
[英]react hooks and typescript - Property '***' does not exist on type 'never'
[英]React Hooks with typescript: Property 'data' does not exist on type
我有一個沒有意義的錯誤,我正在用鈎子輸入我的 state 的值,但他說錯誤不是同一類型。
已經嘗試過使用空數組,甚至是帶有一些數據的數組,並且總是錯誤是相同的。
import React, { useState } from 'react';
import { Row, Col } from 'config/styles';
import Bed from './Bed';
interface DataTypes {
date: string;
value: number;
}
function Beds(): JSX.Element {
const { data, setData } = useState<DataTypes[]>([]);
return (
<>
<Row>
{data.map((d, i) => (
<Col key={i} sm={16.666} lg={10}>
<Bed {...d} />
</Col>
))}
</Row>
</>
);
}
export default Beds;
錯誤:
TypeScript error in /Users/keven/Documents/carenet/orquestra-frontend/src/Beds/index.tsx(11,11):
Property 'data' does not exist on type '[DataTypes[], Dispatch<SetStateAction<DataTypes[]>>]'
它應該是一個數組,而不是 object:
const [data, setData] = useState<DataTypes[]>([]);
您在錯誤消息中有此指示:
type '[DataTypes[], Dispatch<SetStateAction<DataTypes[]>>]'
你應該使用 const [ data, setData ] 而不是 const { data, setData }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.