15

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;

Erro this:

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[]>>]'
2
  • 10
    Change brackets: const [ data, setData ] = ... 😎 Commented Sep 28, 2019 at 8:48
  • Ooooomg :( haha thanks, sorry! Commented Sep 30, 2019 at 13:33

2 Answers 2

19

you should use const [ data, setData ] instead of const { data, setData }

Sign up to request clarification or add additional context in comments.

1 Comment

I cannot believe, what a silly mistake I had, thanks by the way.
9

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

1 Comment

The error is always the same regardless of type. Property 'data' does not exist on type '[[], Dispatch<SetStateAction<[]>>]'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.