reactjs - 如何渲染 SweetAlert React
问题描述
当用户单击注销按钮时,我正在尝试使用 SweetAlert 作为警报。我就是这样做的。SweetAlert(从他们的仓库复制的示例):
const signOutUser = () => {
return (
<SweetAlert
warning
showCancel
confirmBtnText="Yes, delete it!"
confirmBtnBsStyle="danger"
title="Are you sure?"
onConfirm={() => console.log('hey')}
onCancel={() => console.log('bye')}
focusCancelBtn
>
You will not be able to recover this imaginary file!
</SweetAlert>
)}
这就是我试图称呼它的方式:
const Border = () => (
...
<a onClick={signOutUser}/>
...
)
问题是当我点击它时,什么也没有发生。有任何想法吗?
解决方案
您的SweetAlert
组件需要始终呈现(特定情况除外)。触发 SweetAlert 的是show
prop,它是一个Boolean。
您可以将show
道具绑定到组件的状态。让我给你看一个例子:
export default function YourAlert() {
const [isOpen, setOpen] = useState(false);
return (
<SweetAlert
warning
showCancel
show={isOpen} //Notice how we bind the show property to our component state
confirmBtnText="Yes, delete it!"
confirmBtnBsStyle="danger"
title="Are you sure?"
onConfirm={() => console.log("hey")}
onCancel={() => {
console.log("bye");
setOpen(false); // Don't forget to close the modal
}}
focusCancelBtn
>
You will not be able to recover this imaginary file!
</SweetAlert>
<Button
onClick={()=>{
setOpen(true); // Open the modal
}}
>Open the alert</Button>
);
}
注意我评论的地方,因为它会让你理解实现。
推荐阅读
- php - Php Mail wont work on Google App Engine Flex (no errors displayed)
- c# - 收集二维数组并保存到文件
- ruby-on-rails - 向我的 oauth 提供者发出 post 请求时出现 invalid_grant 错误
- java - 如何控制 Spring Boot 启动时的异常?
- powershell - 将两个 csv 合并为第三个 csv:来自两个域的用户和多个代理地址
- arrays - 遍历数组并仅将每个元素中的一个推送到另一个数组
- java - 获取当前登录用户
- android - 从不同的视图开始并以另一个视图结束的阅读手势
- mysql - 设置默认文件后 MySQL 没有启动
- asp.net-web-api2 - 如何使用来自数据源的增量更改来填充和同步索引?