[英]setting state for the array which is inside the object in reactjs
我正在嘗試做setState我想在reactjs中更新狀態。
this.state = {
activePage: 0,
visible: false,
questionType: null,
form: {
title: '',
description: '',
pages: [{
title: '',
description: '',
questions: []
}]
}
};
這是初始狀態,並且在數據的每個輸入上,我希望所有這些問題都在頁面中存在的問題數組下。 為了達到這個目的,我試圖做些什么?
onSubmit = (data) => {
console.log('data is ', data);
let newForm = {
...this.state.form
};
// it shows that we can't do this as it is not iterable
newForm.pages.questions = [...newForm.pages.questions, ...data];
this.setState({
newForm
});
}
這部分代碼無法使用,因為pages
是一個數組。 您必須提供要修改的頁面的索引。
newForm.pages.questions=[...newForm.pages.questions,...data];
應該是這樣的
newForm.pages[indexOfPage].questions=[...newForm.pages[indexOfPage].questions,...data];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.