I'm facing a very weird issue here. I'm using react-toggle dependency to just return true/false values of some inputs.
Basically what I'm doing is that I'm using the callback function of the react-toggle inputs to update the values of my react state. For some reason it will only update the last input that have been changed and the others will go false.
Here is how my code looks:
RandomComponent.js
<ToggleComponent title="TestA"/>
<ToggleComponent title="TestB"/>
<ToggleComponent title="TestC"/>
<ToggleComponent title="TestD"/>
ToggleComponent.js
constructor(props){
super(props);
this.state={
testA:false,
testB:false,
testC:false,
testD:false,
}
}
updateCheckValue(title){
if(title === 'TestA'){
this.setState({
testA: !this.state.testA
}, ()=>{
console.log(this.state)
})
}
if(title === 'TestB'){
this.setState({
testB: !this.state.testB
}, ()=>{
console.log(this.state)
})
}
if(title === 'TestC'){
this.setState({
testC: !this.state.testC
}, ()=>{
console.log(this.state)
})
}
if(title === 'TestD'){
this.setState({
testD: !this.state.testD
}, ()=>{
console.log(this.state)
})
}
}
render(){
return(
<Row className={styles.container}>
<Col sm={1} md={1} lg={1}></Col>
<Col sm={2} md={2} lg={2}>
<Toggle
onChange={this.updateCheckValue.bind(this, this.props.title)}
icons={false} />
</Col>
<Col sm={8} md={8} lg={8}>
<h4>{this.props.title}</h4>
</Col>
<Col sm={1} md={1} lg={1}></Col>
</Row>
)
}
You should change the approach here, the state should be managed by the RandomComponent.js, ToggleComponent.js should call a callback every time it's called. The result should be something like this: RandomComponent.js
constructor(props){
super(props);
this.state={
testA:false,
testB:false,
testC:false,
testD:false,
}
}
updateCheckValue(att){
this.setState({
[att]: !this.state[att]
}, ()=>{
console.log(this.state)
})
}
}
render(){
<ToggleComponent title="TestA" onToggle={() => this.updateCheckValue("testA")}/>
<ToggleComponent title="TestB" onToggle={() => this.updateCheckValue("testB")/>
<ToggleComponent title="TestC" onToggle={() => this.updateCheckValue("testC")/>
<ToggleComponent title="TestD" onToggle={() => this.updateCheckValue("testD")/>
}
ToggleComponent.js
<Row className={styles.container}>
<Col sm={1} md={1} lg={1}></Col>
<Col sm={2} md={2} lg={2}>
<Toggle
onChange={this.props.onToggle}
icons={false} />
</Col>
<Col sm={8} md={8} lg={8}>
<h4>{this.props.title}</h4>
</Col>
<Col sm={1} md={1} lg={1}></Col>
</Row>