我在项目中使用react-js。
const SweetAlert = require('react-sweetalert2');
const Updater = React.createClass({
<div style={{ padding: "20px" }}>
<a onClick={() => this.deleteThisGoal()} className="btn btn-danger"> <i className="fa fa-trash" aria-hidden="true" />
Delete Goal
</a>
{this.state.alert}
</div>
deleteThisGoal: function() {
const getAlert = (
<SweetAlert success title="Woot!">
Hello world!
</SweetAlert>
);
this.setState({
alert: getAlert
});
}
});
当我检查元素时,该值将分配给this.state.alert
,但是当我单击警报时,不会显示弹出窗口。
有人能指出我正确的方向吗?
我以Add Sweet Alert popup to button in React component为例。
将我的答案更新为:
const SweetAlert = require('react-sweetalert2');
const Updater = React.createClass({
render(
<div>
<div style={{ padding: '20px' }}> <a onClick={this.deleteThisGoal} className='btn btn-danger' > Delete Goal </a>
</div>
{this.popup}
</div>
);
},
deleteThisGoal: function() {
this.setState({
show: true
});
},
popup: function(keyword) {
return (
<SweetAlert
show={this.state.show} title="Deleted goal" text="Goal was deleted!"
onConfirm={() => this.setState({ show: false })}>
</SweetAlert>
);
}
});
我仍然看不到任何警报。
答案 0 :(得分:0)
我通过使用const SweetAlert = require('react-sweetalert2').default;