varung1303
varung1303

Reputation: 47

How do I define an array of objects in a state in React Typescript?

I need to initialize an array of objects in my state in react Ts. The array should look somewhat like this

  addressBook[
       details{
           name:"Tom"
           mail: "[email protected]"
           mobile: "1111111111"
       }
   ]

I need to create an array of objects like this which will contain multiple contacts with their details.

Upvotes: 1

Views: 926

Answers (1)

Giovanni Esposito
Giovanni Esposito

Reputation: 11156

Define an interface:

interface Detail {
  name: string;
  mail: string;
  mobile: string;
}

Then in component definition:

const DetailPage = (): ReactElement => {
   const [details, setDetails] = useState<Detail[]>([]);

   return ( . . . );
};

Upvotes: 2

Related Questions