![](/img/trans.png)
[英]Uncaught TypeError: Cannot assign to read only property 'background' of object '#<Object>'
[英]TypeError: Cannot assign to read only property 'statusKnown' of object '#<Object>'
我正在嘗試根據我在基於反應 Class 的組件中的道具更新 state 但我收到錯誤消息
TypeError:無法分配給 object '#' 的只讀屬性 'statusKnown'
這是下面的代碼
componentDidMount(){
this.calClass()
}
calClass = () => {
console.log(this.props.statusKnown, this.state.active);
if (this.props.statusKnown == "deactive") this.setState({ active: false });
else if ((this.props.statusKnown = "active")) this.setState({ active: true });
};
然后在父組件中
<Button item={item} categoryID={categoryID} statusKnown={status}/>;
如果反應不允許這件事,那么可能的解決方案是什么?
你錯過了一些=
標志。 你應該總是使用===
componentDidMount(){
this.calClass()
}
calClass = () => {
console.log(this.props.statusKnown, this.state.active);
if (this.props.statusKnown === "deactive") this.setState({ active: false });
else if ((this.props.statusKnown === "active")) this.setState({ active: true });
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.