I have 5 different buttons, what I wanted to do is when a certain button is clicked, for example button 1 then state one will return true however buttons two, three, four and five will return false.
What i wanted to achieve is to replace the className of buttons when a certain button is clicked.
Now on my code, when button is clicked, the value I'm passing will setState to true. Then will filter the data array and set the return to 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>
)
}
})
Thanks :D