在Javascript中的狀態,string, integer and boolean values are immutable, but objects and arrays are mutable.陣營:如何更新既具有可變和不可變值
我們應該如何更新狀態的反應,如果國家有兩種類型的值嗎?
例如
constructor(props) {
super(props);
this.state = {
success: false,
error: false,
errorMessages: {}
};
}
假設你需要upgdate一次全部屬性(success
,error
,errorMessages
)的,這將是實現這一目標最好的方式?
至少我確定errorMessages
不應該直接更新,因爲它本質上是可變的,但其他人呢?
我嘗試了類似下面的內容,但是這樣做的結果是錯誤的。
const errorMessages = {
...this.state,
"errorMessages": error.response.data,
};
this.setState({
errorMessages,
success: false,
error: true,
});
//The errorMessages property will have "success" and "error" property in it
價值是可變的或不可變的沒有任何關係的陣營與狀態有關。 –