0
我打算渲染元素或僅在單擊按鈕時使元素可見。但元素無論如何都會呈現。儘管日誌說狀態是假即使狀態可見性爲假,圖像也會呈現
getInitialState: function(){
return {
number: 0,
'image1': "https://www.bignerdranch.com/img/blog/2014/07/Button-2.png",
visible1: false
}
},
add: function(){
var number = this.state.number;
number = number+1;
console.log(number);
var temp = "visible"
temp = temp + number
var s ={};
s[temp] = true;
console.log(s);
this.setState(s);
},
render: function(){
return(
<div>
<img className={"addpicture "+this.state.visible1?"visible":"invisible"} onClick={this.camera} src={this.state.image1} data-cam={1}/>
<button onClick={this.add}>Click me</button>
{console.log(this.state.visible1)}
</div>
);
}
太棒了!如何避免這類錯誤?如何在這些情況下編寫安全的代碼?任何建議。我是新來的JS – gates
只要確保你瞭解你的表達式如何評估。瞭解評估的順序和類型強制的東西。例如,'1 +'字符串'或'+'1''會發生什麼。這可能會很棘手! – Aweary