M
M
miliko mikoyan2019-06-10 14:26:08
typescript
miliko mikoyan, 2019-06-10 14:26:08

Why can't the initial value of hooks be set to an empty object?

I have this setProd Hooks

export interface faceProduct {
  readonly title: string;
  readonly prodState: string;
  readonly shipping: string;
  readonly sold: string;
  readonly alt: string;
  readonly material: string;
  readonly location: string;
  readonly src: string[];
  readonly color: string[];
  readonly saiz: string[];
  readonly price: string;
}

export interface faceProductList extends faceProduct {
  readonly id: string;
  readonly to: string;
}

 const [prod, setProd] = useState<faceProductList>({});

I want the initial values ​​to be an empty object. But I get an error..
But if write.
const [prod, setProd] = useState<faceProductList>(Object);

everything works with what it is connected.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-06-10
@miliko0022

what is it connected with

with the same as:
let s: string;

s = 182;

You can fix it like this:
const [prod, setProd] = useState<FakeProductList | null>(null);

if (!prod) {

}

return (/* ... */);

Please note that interfaces are usually capitalized, and the word fake is written with the letter k .
The readonly attribute is redundant.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question