带有打字稿的 React Hooks:类型上不存在属性“数据" [英] React Hooks with typescript: Property 'data' does not exist on type

查看:20
本文介绍了带有打字稿的 React Hooks:类型上不存在属性“数据"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个毫无意义的错误,我正在用钩子输入我的状态的值,但他说错误是不同的类型.

I have an error that makes no sense, I am typing the value of my state with hooks, but he says the error is not the same type.

已经尝试过空数组,甚至是带有一些数据的数组,但总是错误相同.

Already tried with empty array and even array with some data and always error is the same.

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[]>>]'

推荐答案

它应该是一个数组,而不是一个对象:

It should be an array, not an object:

const [data, setData] = useState<DataTypes[]>([]);

您在错误消息中有此指示:

You have this indication in the error message:

type '[DataTypes[], Dispatch<SetStateAction<DataTypes[]>>]'

这篇关于带有打字稿的 React Hooks:类型上不存在属性“数据"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆