0
我已經得到了關於這些兩個選項呈現行爲問題:陣營的setState呈現行爲
export function saveOrCancelStepContentComponents(stepIndex, targetArray, newArray) {
// Option 1
let stepsData = this.state.stepsData.slice();
stepsData[stepIndex][targetArray] = newArray;
this.setState({stepsData: stepsData},() => {
console.log("saveStepConditions[]:", this.state.stepsData[stepIndex][targetArray]);
});
// Option 2
this.setState((prevState) => ({
stepsData: prevState.stepsData.map((currentStep, i) => {
if (i === stepIndex) {
return {
...currentStep,
[targetArray]: newArray,
}
}
return currentStep
})
}),() => {
console.log("saveStepConditions[]:", this.state.stepsData[stepIndex][targetArray]);
});
}
取消和保存有兩個選項,這是說,console.logs是精品。但是,只有選項2纔會重新呈現頁面。選擇1將不會導致任何表型變化。 任何想法,爲什麼?謝謝!
謝謝你,男人! – Nocebo
所以如果我想拼接一個元素,我會做同樣的事情嗎? 'stepsData [stepIndex] .contentComponents = { ... stepsData [stepIndex], contentComponents:stepsData [stepIndex] .contentComponents.splice(I,1) };' – Nocebo
@Nocebo忘掉'splice'。它會改變原始數組並返回拼接的元素。使用'filter'代替。 –