在我的專案中,我有登錄表單,所有元素都保存在陣列中,如下所示:
const inputs: [*****] = [
<Form.Group>
<Form.Label>username</Form.Label>
<Form.Control
placeholder="enter username"
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setUsername(e.target.value)
}
value={username}
></Form.Control>
</Form.Group>,
<Form.Group>
<Form.Label>password</Form.Label>
<Form.Control
placeholder="enter password"
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setPassword(e.target.value)
}
value={password}
></Form.Control>
</Form.Group>,
<Button variant="primary">Login</Button>,
];
我想用 map 函式渲染這個陣列,但我不知道我需要在我放的 **** 上寫什么型別。我怎么知道型別?
uj5u.com熱心網友回復:
如果您使用的是 Visual Studio Code,則可以洗掉該型別,然后單擊Ctrl.變數上方的重構快捷方式 ( )。彈出選單后,單擊Infer type(或類似的),它將自動設定型別。
但是,由于它是組件串列,您可以嘗試以下型別:React.ReactNode[]
請注意,Typescript 陣列不是用 , 定義的
[<type-name>],而是<type-name>[].
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/464710.html
