簡體   English   中英

如何在 React Typescript 中的 state 中定義對象數組?

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

我需要在反應 Ts 中初始化我的 state 中的對象數組。 數組應該看起來像這樣

  addressBook[
       details{
           name:"Tom"
           mail: "xyz@g.com"
           mobile: "1111111111"
       }
   ]

我需要創建一個像這樣的對象數組,其中將包含多個聯系人及其詳細信息。

定義一個接口:

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

然后在組件定義中:

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

   return ( . . . );
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM