3 回答
TA贡献1858条经验 获得超8个赞
setState()有一个可选的第二个参数,它是一个函数,一旦设置了状态就会被调用。你可以这样做:
this.setState({ Operator: 'plus' }, this.handlePlus);
您还可以使用componentDidUpdate生命周期:
componentDidUpdate(prevProps, prevState) {
if(this.state.Operator === 'plus' && prevState.Operator !== 'plus') {
this.handlePlus();
}
}
TA贡献1829条经验 获得超4个赞
< button name = "Plus"
onClick = {
() => this.setState({
Operator: 'plus'
}, () => this.handlePlus())
} > + < /button>
this would work fine.or
if you are looking
for something like this....
you can handle multiple buttonClick event like this
<
button name = "Plus"
value = "yourValue"
onClick = {
this.handleChange
} > + < /button>
handleChange = (e) => {
const {
name,
value
} = e.target;
switch (name) {
case 'Plus':
this.setState({
[name]: value
});
break;
default:
//enter code here
break;
}
}
添加回答
举报