我为 Bootstrap 和 React 找到了这个完美的 Sweet Alert 模块(我在 Meteor 应用程序中使用它):
http://djorg83.github.io/react-bootstrap-sweetalert/ http://djorg83.github.io/react-bootstrap-sweetalert/
但我不明白你如何将此代码包含在 React 组件中。
当有人单击我的应用程序中的“删除”按钮时,我希望弹出一个甜蜜警报提示,要求确认。
这是我的删除按钮组件:
import React, {Component} from 'react';
import Goals from '/imports/collections/goals/goals.js'
import SweetAlert from 'react-bootstrap-sweetalert';
export default class DeleteGoalButton extends Component {
deleteThisGoal(){
console.log('Goal deleted!');
// Meteor.call('goals.remove', this.props.goalId);
}
render(){
return(
<div className="inline">
<a onClick={this.deleteThisGoal()} href={`/students/${this.props.studentId}/}`}
className='btn btn-danger'><i className="fa fa-trash" aria-hidden="true"></i> Delete Goal</a>
</div>
)
}
}
这是我从 Sweet Alert 示例中复制的代码:
<SweetAlert
warning
showCancel
confirmBtnText="Yes, delete it!"
confirmBtnBsStyle="danger"
cancelBtnBsStyle="default"
title="Are you sure?"
onConfirm={this.deleteFile}
onCancel={this.cancelDelete}
>
You will not be able to recover this imaginary file!
</SweetAlert>
有人知道怎么做吗?
基于您的代码的工作示例http://www.webpackbin.com/VJTK2XgQM http://www.webpackbin.com/VJTK2XgQM
你应该使用this.setState()
并创造<SweetAlert ... />
on onClick
。您可以使用粗箭头或.bind()
或任何其他方法来确保使用正确的上下文。
import React, {Component} from 'react';
import SweetAlert from 'react-bootstrap-sweetalert';
export default class HelloWorld extends Component {
constructor(props) {
super(props);
this.state = {
alert: null
};
}
deleteThisGoal() {
const getAlert = () => (
<SweetAlert
success
title="Woot!"
onConfirm={() => this.hideAlert()}
>
Hello world!
</SweetAlert>
);
this.setState({
alert: getAlert()
});
}
hideAlert() {
console.log('Hiding alert...');
this.setState({
alert: null
});
}
render() {
return (
<div style={{ padding: '20px' }}>
<a
onClick={() => this.deleteThisGoal()}
className='btn btn-danger'
>
<i className="fa fa-trash" aria-hidden="true"></i> Delete Goal
</a>
{this.state.alert}
</div>
);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)