我有5個不同的按鈕,當點擊某個按鈕,例如button 1
然後state one
將返回true
然而buttons two, three, four and five
將返回false
我想要做的是。 我想要實現的是在單擊某個按鈕時替換按鈕的className。陣營:如何篩選陣列的onClick隨後的setState未選擇按鈕
現在在我的代碼中,當單擊按鈕時,我傳遞的值將setState設置爲true。然後將過濾數據數組並將返回值設置爲false。
import React from 'react'
var data = ["One", "Two", "three", "four", "five"]
export default React.createClass({
getInitialState(){
return{
one: false,
two: false,
three: false,
four: false,
five: false
}
},
onButtonClick(name){
var filter = data.filter((item) => {return item != name})
var obj = {}
obj[name] = true
this.setState(obj)
console.log(filter)
// can I setState the unclicked buttons from var filter?
},
render(){
return(
<div>
<button id="one" onClick={this.onButtonClick.bind(this, "one")} className={!this.state.one ? "btn btn-block btn-default" : "btn btn-block btn-info"}>one</button>
<button id="two" onClick={this.onButtonClick.bind(this, "two")} className={!this.state.two ? "btn btn-block btn-default" : "btn btn-block btn-info"}>two</button>
<button id="three" onClick={this.onButtonClick.bind(this, "three")} className={!this.state.three ? "btn btn-block btn-default" : "btn btn-block btn-info"}>three</button>
<button id="four" onClick={this.onButtonClick.bind(this, "four")} className={!this.state.four ? "btn btn-block btn-default" : "btn btn-block btn-info"}>four</button>
<button id="five" onClick={this.onButtonClick.bind(this, "five")} className={!this.state.five ? "btn btn-block btn-default" : "btn btn-block btn-info"}>five</button>
</div>
)
}
})
感謝:d
真棒,謝謝:)這是比我更簡潔 – kcNeko