[英]How do I create a new object from states in React with typescript?
我有一個 state 在反應中包含一組文件。 如何改為創建 DocumentRequestModel 類型的新 object? 這是我到目前為止得到的。 文件是我的 state 包含我的文件。
export class DocumentRequestModel implements IDocumentRequestModel {
Data!: string;
FileName!: string;
...
這是我到目前為止得到的,但我收到錯誤消息:Type '(string | undefined)[][] | undefined' 不可分配給類型 'DocumentRequestModel'。 類型“未定義”不可分配給類型“DocumentRequestModel”。
const documentModel: DocumentRequestModel = documents?.map(document => [
document.preview,
document.name
]);
這就是文檔的外觀。
0: File
name: "BOM_BR4666_PROJECTTYPE F_04_06_2020.xlsx"
lastModified: 1586170093516
lastModifiedDate: Mon Apr 06 2020 12:48:13 GMT+0200 (Central European Summer Time) {}
webkitRelativePath: ""
size: 3948
type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
preview: "blob:http://localhost:4000/6d4be14b-0c09-45f9-b2c3-50
你可以這樣做:
const [docModel,setDocModel] = React.useState<DocumentRequestModel>()
或者你想在數組中定義它
const docModel = new Array<DocumentRequestModel>()
在您的映射中,文檔可能未定義。 您可能想在調用 map function 之前檢查它是否未定義
const documentModel: Array<DocumentRequestModel> = documents && documents.map(document => [
document.preview,
document.name
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.