簡體   English   中英

將對象數組映射到狀態

[英]Mapping an object array into a state

我有一個API以這種形式給出錯誤響應 在此處輸入圖片說明

在reactJS中,我可以使用this.props.errors傳遞參數給子組件,但是現在我想按如下所示將this.props.errors映射到子組件

state = {
 slug: '', 
 name: '', 
 address_1: '', 
 address_2: '', 
 city: '', 
 state: '', 
 postal_code: '', 
 country_code: '', 
 phone_number: '',
}

意思是,我希望將error參數的元素放在每個元素的子組件狀態中。 我怎么做?

根據您的要求保存您的響應以進行定制。 按照代碼段。 將其保存為狀態,然后將此狀態傳遞給孩子。 通過使用componentWillReceiveProps更新子狀態。

示例: 在React中使用代碼

 let response = { slug: ['slug required'], name: ['name'], address_1: ['address_1'], address_2: ['address_2'], city: ['city'], state: ['state'], postal_code: ['postal_code'], country_code: ['country_code'], phone_number: ['phone_number '] } const keys = Object.keys(response) keys.map((m)=>{ response[m] = Array.isArray(response[m]) ? response[m][0] : response[m] }) console.log("customed response",response) 

暫無
暫無

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

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