[英]React.js Onclick event on a dynamically generated button is not firing
[英]onClick not firing withing a SweetAlert modal in React.js
我正在使用SweetAlert来处理我的React.js 应用程序中的模式,并且我有一个用于这个特定模式的主体的功能组件。 它使用一个简单的 state 挂钩来增加/减少两者之间的数值。 问题是, onClick 事件根本没有触发。 我已经尝试以多种方式对此进行调试,但均未成功。 任何帮助表示赞赏。
export default class FoodItem extends React.Component{
constructor(props){
super(props);
this.state ={
loading:false,
addedPrice:0
}
this.openOptions = this.openOptions.bind(this);
}
componentDidMount(){
this.setState({
addedPrice:this.props.cena
});
}
openOptions(){
swal({
title:this.props.jedlo,
buttons:{
cancel:{
text:'Zrusit',
value:null,
visible:true,
closeModal:true
},
confirm:{
text:'Pridat ' + this.state.addedPrice,
value:true,
visible:true,
closeModal:true
}
},
content:(
<div>
<ModalBody />
</div>
)
});
}
render(){
return(
<button className='food-item' type="button" onClick={this.openOptions}>
<p className='food_text'>{this.props.jedlo}</p>
<p className='price_text'>{this.props.cena} €</p>
</button>
)
}
}
function ModalBody(){
const [num, changeNum] = useState(1);
return(
<div className='increaseNumItems'>
<button type='button' className='btnValueChange' onClick={() => changeNum(num - 1)}>-</button>
<p id='valueTimes'>{num}</p>
<button type='button' className='btnValueChange' onClick={() => changeNum(num + 1)}>+</button>
</div>
)
}
经过数小时的研究,我得出的结论是,这个特别的 sweetalert package 坏了,我建议使用 SweetAlert2 一切似乎工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.