【发布时间】:2020-07-30 08:13:02
【问题描述】:
我的函数 handleAddPoint 接收这样的数据:
data = {colorid,counter}
我从名为“Qualificatif”的子组件调用此函数,该子组件仅根据 Select 输入中的 onChange 事件推送数据。
每次我从我的选择输入中更改值时,名为 handleAddPoint 的函数内的映射函数在 if 条件下执行两次。
你能帮帮我吗?
代码如下:
export default class Questionnaire extends Component {
constructor(props){
super(props)
this.state ={
colors:[
{
id:1,
color:"yellow",
point:0
},
{
id:2,
color:"green",
point:0
},
{
id:3,
color:"red",
point:0
},
{
id:4,
color:"blue",
point:0
},
],
}
this.handleAddPoint = this.handleAddPoint.bind(this)
}
handleAddPoint = (data) =>{
console.log("Data questionnaire",data)
this.setState(state => {
state.colors.map(color =>
{if(color.id === data.colorid)
color.point += data.counter
return color
})
})
console.log("couleurs questionnaire",this.state.colors)
}
render() {
return (
<div className="questionnaire">
<Qualificatif data={Q1data} handleaddpoint={this.handleAddPoint}/>
<Qualificatif data={Q2data} handleaddpoint={this.handleAddPoint}/>
<Classement data={Q3data} handleaddpoint={this.handleAddPoint}/>
<Result data={this.state.colors}/>
</div>
)
}
}
【问题讨论】:
标签: reactjs function dictionary if-statement state