【问题标题】:Prevent the display of the alert `swal` (sweetalert) after the component has loaded防止在组件加载后显示警报 `swal` (sweetalert)
【发布时间】:2019-10-29 11:53:33
【问题描述】:

当您单击事件和组件加载后都会出现 swal 警报。如何将 swal 警报设置为仅在单击事件后触发,而不是在每次刷新组件后触发。

在这里演示:https://stackblitz.com/edit/react-2dmw2w

function Event({ event }) {

  swal({
    title: event.title,
    text: `${event.description} ${event.id}`,
    /*icon: "info",*/
    buttons: true,
    dangerMode: true,
  })
  .then((willDelete) => {
    if (willDelete) {
      swal("Poof! Your imaginary file has been deleted!", {
        icon: "success",
      });
    } else {
      swal("Your imaginary file is safe!");
    }
  });

///////

  <Calendar
    events={this.state.events}
    startAccessor="start"
    endAccessor="end"
    defaultDate={moment().toDate()}
    localizer={localizer}
    components={{
      event: Event
    }}
  />

【问题讨论】:

  • 在演示中看不到任何内容,您介意确保设置正确吗?

标签: javascript reactjs sweetalert react-datepicker


【解决方案1】:

我对您的代码进行了一些修改,应用了 ES6(用于最佳实践)并修改了几个变量(我认为大写 E 的事件是保留字)

https://stackblitz.com/edit/react-gd4kqm

但是为了达到您想要的效果,我所做的只是在您单击日历中的事件时才调用 SWAL,我将其分为两个功能,如果您有任何问题,请提出。

JS中的保留字列表:

https://www.w3schools.com/JS/js_reserved.asp

【讨论】:

  • 我们可以聊聊吗?
  • 是的,但我不知道该怎么做
  • 有时间我会去看看,同时如果这个答案和解释适合你,请采纳答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
相关资源
最近更新 更多